Program prints -ve integers after 2147483647
Sizing of integers is platform-specific. C standard does not say exactly how many bits an int
must have, all it says is that an int
must be at least 16-bit long. This is not a restriction on the compiler - rather, it is one of many compiler's property that define your platform.
The bit-ness of your platform does not have a direct impact on the size of the int
on your platform. You can often use the same compiler to generate 32-bit and 64-bit code (with gcc, you can use flags -m32 or -m64), but the sizes of built-in types will not necessarily change.
If you need bigger range for your integers, you can use long long
, which is guaranteed to have at least 64 bits. In addition, gcc supports 128-bit integer type __int128
. If you need even more range, you can use an arbitrary precision math package.
Why does trying to go up to 2,147,483,647 with a for-loop result in an infinite loop?
There is no output because the for loop never terminates. You have to change the condition from for (int roll = 1; roll <= num; roll++)
to for (int roll = 0; roll < num; roll++)
.
In your case, roll reaches Integer.MAX_VALUE, the condition is still true, so the loop is entered once more. Now roll is increased, overflows to Integer.MIN_VALUE, and is still <= num
. Hence, the for loop never stops.
In addition, you'll run into problems with num
being float
, see Eran's answer for an explanation.
Why I get -2 if I add 2147483647 to itself in java for int and long type?
Edit: live demo
Here's what's going on when you add those numbers:
int a = 2147483647;
int b = 2147483647;
In binary:
a = 0b01111111111111111111111111111111
b = 0b01111111111111111111111111111111
Adding those numbers together gives a number larger than Integer.MAX_VALUE
(obviously):
long c = a + b; // -2
In binary:
0b01111111111111111111111111111111
+ 0b01111111111111111111111111111111
------------------------------------
0b11111111111111111111111111111110
== -2 when taking into account 2's complement
The result is then upcast to a long
.
To ensure the numbers are added as long
s and not int
s, make a
and b
both long
s.
Is `long` guaranteed to be at least 32 bits?
The answer is definitively YES. Read my OP and all the comments to understand why exactly, but here's the short version. If you doubt or question any of this, I encourage you to read the entire thread and all of the comments. Otherwise accept this as true:
- The C++ standard includes parts of the C standard, including the definitions for
LONG_MIN
andLONG_MAX
LONG_MIN
is defined as no greater than-2147483647
LONG_MAX
is defined as no less than+2147483647
- In C++ integral types are stored in binary in the underlying representation
- In order to represent
-2147483647
and+2147483647
in binary, you need 32 bits. - A C++ long is guaranteed to be able to represent the minimum range
LONG_MIN
throughLONG_MAX
Therefore a long
must be at least 32 bits1.
EDIT:
LONG_MIN
and LONG_MAX
have values with magnitudes dictated by the C standard (ISO/IEC 9899:TC3) in section §5.2.4.2.1:
[...] Their implementation-defined values shall be equal or greater in magnitude [...] (absolute value) to those shown, with the same sign [...]
— minimum value for an object of type long int
LONG_MIN -2147483647 // -(2 ^ 31 - 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 2 ^ 31 - 1
1 32 bits: This does not mean that sizeof (long) >= 4
, because a byte is not necessarily 8 bits. According to the Standard, a byte is some unspecified (platform-defined) number of bits. While most readers will find this odd, there is real hardware on which CHAR_BIT
is 16 or 32.
BooleanArray with size larger than 2147483647
The most easy might be to use UInt
as the size and then internally map 2 common BooleanArray
, which use Int
for addressing, despite there never could be a negative index value (this seems to be a design fault - or at least a lack of optimization). Which means, that one could as well address everything with signed Int
. I mean, to internally map the negative values to one BooleanArray
and the positive values to another one BooleanArray
. The actual problem seems to be, that a signed Int
is being passed, but only the positive range (50%) can be used to address data. One could use absoluteValue
, because it doesn't matter in which direction the array is being filled.
Related Topics
Undefined Symbols for Architecture X86_64: Which Architecture Should I Use
Best Way to for C++ Types to Self Register in a List
How to Set Error_Code to Asio::Yield_Context
What Is the Meaning of & in C++
Qmetaobject::Connectslotsbyname: No Matching Signal
Why Does C++11 Not Support Anonymous Structs, While C11 Does
Class' Keyword in Variable Definition in C++
C++: Calling Member Function via Pointer
How to Detect Text Area from Image
C++ Copy Constructor Gets Called Instead of Initializer_List<>
Exporting Static Data in a Dll
Reading a Matrix Txt File and Storing as an Array