Questions tagged [integer]

Common datatype in many programming languages for representing both negative and non-negative whole numbers. Use this tag for questions about using, storing, or manipulating integers. For 64-bit integers, use the [long-integer] tag instead.

An integer is a whole number that can be negative, positive, or zero. For example, -2, -1, 0, 1, 2. In many programming languages, the integer data type is commonly represented as 32-bits, but may also be 64-bits (Usually referred to as the Long data type).

For 32-bit integers:

  • The signed range is : -2147483648 to 2147483647.
  • The unsigned range is : 0 to 4294967295.

For 64-bit integers:

  • The signed range is : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • The unsigned range is : 0 to 18,446,744,073,709,551,615.

For further reading, see the Wikipedia article on integers.

13448 questions
403
votes
17 answers

Why doesn't Java support unsigned ints?

Why doesn't Java include support for unsigned integers? It seems to me to be an odd omission, given that they allow one to write code that is less likely to produce overflows on unexpectedly large input. Furthermore, using unsigned integers can…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
397
votes
31 answers

Converting String to Int with Swift

The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the values in the text boxes are string, I am unable to convert them to…
Marwan Qasem
  • 3,981
  • 2
  • 12
  • 4
397
votes
8 answers

How do you express binary literals in Python?

How do you express an integer as a binary number with Python literals? I was easily able to find the answer for hex: >>> 0x12AF 4783 >>> 0x100 256 and octal: >>> 01267 695 >>> 0100 64 How do you use literals to express binary in Python? Summary…
Justin Standard
  • 21,347
  • 22
  • 80
  • 89
396
votes
23 answers

Extract a single (unsigned) integer from a string

I want to extract the digits from a string that contains numbers and letters like: "In My Cart : 11 items" I want to extract the number 11.
Bizboss
  • 7,792
  • 27
  • 109
  • 174
394
votes
9 answers

Python strings and integer concatenation

I want to create a string using an integer appended to it, in a for loop. Like this: for i in range(1, 11): string = "string" + i But it returns an error: TypeError: unsupported operand type(s) for +: 'int' and 'str' What's the best way to…
michele
  • 26,348
  • 30
  • 111
  • 168
371
votes
31 answers

How to find length of digits in an integer?

In Python, how do you find the number of digits in an integer?
Strigoides
  • 4,329
  • 5
  • 23
  • 25
371
votes
11 answers

Convert floats to ints in Pandas?

I've been working with data imported from a CSV. Pandas changed some columns to float, so now the numbers in these columns get displayed as floating points! However, I need them to be displayed as integers or without comma. Is there a way to convert…
MJP
  • 5,327
  • 6
  • 18
  • 18
365
votes
11 answers

How can I convert an int to a string in C?

How do you convert an int (integer) to a string? I'm trying to make a function that converts the data of a struct into a string to save it in a file.
user1063999
  • 3,689
  • 3
  • 14
  • 5
335
votes
11 answers

max value of integer

In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767. In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647. I do not understand how the range is different in Java, even…
stackuser
  • 4,081
  • 4
  • 21
  • 27
310
votes
17 answers

Convert a comma-delimited string into array of integers?

The following code: $string = "1,2,3" $ids = explode(',', $string); var_dump($ids); Returns the array: array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } I need for the values to be of type int instead of type…
cwal
  • 3,732
  • 3
  • 19
  • 20
300
votes
10 answers

How can I properly compare two Integers in Java?

I know that if you compare a boxed primitive Integer with a constant such as: Integer a = 4; if (a < 5) a will automatically be unboxed and the comparison will work. However, what happens when you are comparing two boxed Integers and want to…
Samatha84
296
votes
20 answers

Mapping two integers to one, in a unique and deterministic way

Imagine two positive integers A and B. I want to combine these two into a single integer C. There can be no other integers D and E which combine to C. So combining them with the addition operator doesn't work. Eg 30 + 10 = 40 = 40 + 0 = 39 +…
harm
  • 10,045
  • 10
  • 36
  • 41
285
votes
10 answers

How do I work around JavaScript's parseInt octal behavior?

Try executing the following in JavaScript: parseInt('01'); //equals 1 parseInt('02'); //equals 2 parseInt('03'); //equals 3 parseInt('04'); //equals 4 parseInt('05'); //equals 5 parseInt('06'); //equals 6 parseInt('07'); //equals 7 parseInt('08');…
Portman
  • 31,785
  • 25
  • 82
  • 101
284
votes
13 answers

Is there a way to iterate over a range of integers?

Go's range can iterate over maps and slices, but I was wondering if there is a way to iterate over a range of numbers, something like this: for i := range [1..10] { fmt.Println(i) } Or is there a way to represent range of integers in Go like…
Vishnu
  • 4,377
  • 7
  • 26
  • 40
279
votes
9 answers

Safest way to convert float to integer in python?

Python's math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import…
Boaz
  • 25,331
  • 21
  • 69
  • 77