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
5
votes
3 answers

Why are there type modifiers for constants?

I can not understand what the purpose is of type modifiers for literal constants, like for numerical constants: 75 75u 75l 75ul 75lu In what cases could this possibly be useful? I mean if you already have declared a type modifier for the…
user7750295
5
votes
2 answers

Optimizing portable 128-bit integer shifts on a 32-bit architecture

In my spare time I've been working on a utility library that among other things, supports signed/unsigned 128-bit integers. This library uses cpu-dispatching to utilize simd instructions in some cases, but requires a portable fallback so it will run…
Koby Duck
  • 1,118
  • 7
  • 15
5
votes
3 answers

short int Integer wrap around / short int inversion in c not understood, difference between assignment and prints

The following code fragment short int k = -32768; printf("%d \n", -k); k=-k; printf("%d \n", k); prints 32768 -32768 I would assume that both prints are equal. Can somebody explain what the difference is and why the assignment k=-k causes a wrap…
Falco Winkler
  • 1,082
  • 1
  • 18
  • 24
5
votes
5 answers

PHP integer rounding problems

echo (int) ( (0.1+0.7) * 10 ); Why does the above output 7? I understand how PHP rounds towards 0, but isn't (0.1+0.7) * 10 evaluated as a float and then casted as an integer? Thanks!
joshim5
  • 2,292
  • 4
  • 28
  • 40
5
votes
2 answers

Inserting commas into integers

In Android is there an easy way to insert commas into a numberical value? ie if I have an EditText with 12345 in it, how can I display this as 12,345? I think I can do it using substring and chopping it up into x number of 3 number chunks then…
Entropy1024
  • 7,847
  • 9
  • 30
  • 32
5
votes
1 answer

Numpy: populate a 2D numpy array with sequential integers

Say you want to create a 2D numpy array, and want to populate it with sequential integers. What are the options available? Example: import numpy a=numpy.array([[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]]) In this case of course I could use range(15)…
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
5
votes
1 answer

Are unsigned integers standard in fortran?

In 2017, are unsigned integers in standard Fortran, or is it still a maybe not portable extension of some compilers?
Anthony Scemama
  • 1,563
  • 12
  • 19
5
votes
3 answers

Cast undefined to 0

Is there a succinct operation in JavaScript (ES2015) that does this: x => x === undefined ? 0 : x I keep running into situations where I want to increment x, but it's initially undefined, so the code becomes: foo.bar = (foo.bar === undefined ? 0 :…
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
5
votes
7 answers

how to calculate (a times b) divided by c only using 32-bit integer types even if a times b would not fit such a type

Consider the following as a reference implementation: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint64_t x = a; x = x * b; x = x / c; return x; } I am interested in an implementation (in C or…
5
votes
2 answers

python int doesn't have __iadd__() method?

I know this is bad practice: >>> a = 5 >>> a.__radd__(5) 10 >>> a 5 >>> a.__iadd__(5) Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute '__iadd__' Out of curiosity, if an int…
Austin Richardson
  • 8,078
  • 13
  • 43
  • 49
5
votes
3 answers

Performance comparisons betweeen enum evaluations and ints

Would there be difference in speed between if (myInt == CONST_STATE1) and if (myEnum == myENUM.State1) in c#?
Adam Naylor
  • 6,172
  • 10
  • 49
  • 69
5
votes
5 answers

Is there any harm in having many enum values? (many >= 1000)

I have a large list of error messages that my biz code can return based on what's entered. The list may end up with more than a thousand. I'd like to just enum these all out, using the [Description("")] attribute to record the friendly…
sohtimsso1970
  • 3,216
  • 4
  • 28
  • 38
5
votes
4 answers

Random number generator that generates integers for Java

I want to generate some random integers in Java, but this according to some distribution laws. More specific: I want to generate some random integers for gaussian distribution. I found out only generators which return double results for the…
som86
  • 61
  • 4
5
votes
1 answer

integer conversion rank and promotion

Reading about the integer promotion and integer conversion rank I found this link 1.If both operands have the same type, then no further conversion is needed. 2.Otherwise, if both operands have signed integer types or both have unsigned integer…
rondino
  • 395
  • 2
  • 3
  • 10
5
votes
2 answers

Convert integer to hex string in Groovy

I'm new to Groovy. When I want convert some integer number to hex string, I have tried codes like this: theNumber.toString(16) as what I did in JavaScript. (Groovy is just like yet another script language looks similar to Java, right?) But the code…
tsh
  • 4,263
  • 5
  • 28
  • 47