Reputation: 2983
I'm working on a system that requires me to be able to set the individual bits that make up an integral type.
I wrote a program to test the usage of some binary operators. The following is a small example that uses the binary OR operator to set bits to true. But the behavior is confusing to me. If I loop through, setting each bit to true, it suddenly sets all bits to true after the 31st bit. I'm not sure why this would be considering I'm on a 64 bit machine and I'm using numeric_limits
to get the number of bits.
#include <iostream>
#include <bitset>
#include <limits>
int main() {
typedef unsigned long long ullong;
ullong bits;
for(int i = 0; i < std::numeric_limits<ullong>::digits; ++i) {
bits |= (1 << i);
std::cout << std::bitset<std::numeric_limits<ullong>::digits>(bits) << "\n";
}
return 0;
}
The output of this program is as follows:
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000011
0000000000000000000000000000000000000000000000000000000000000111
0000000000000000000000000000000000000000000000000000000000001111
0000000000000000000000000000000000000000000000000000000000011111
0000000000000000000000000000000000000000000000000000000000111111
0000000000000000000000000000000000000000000000000000000001111111
0000000000000000000000000000000000000000000000000000000011111111
0000000000000000000000000000000000000000000000000000000111111111
0000000000000000000000000000000000000000000000000000001111111111
0000000000000000000000000000000000000000000000000000011111111111
0000000000000000000000000000000000000000000000000000111111111111
0000000000000000000000000000000000000000000000000001111111111111
0000000000000000000000000000000000000000000000000011111111111111
0000000000000000000000000000000000000000000000000111111111111111
0000000000000000000000000000000000000000000000001111111111111111
0000000000000000000000000000000000000000000000011111111111111111
0000000000000000000000000000000000000000000000111111111111111111
0000000000000000000000000000000000000000000001111111111111111111
0000000000000000000000000000000000000000000011111111111111111111
0000000000000000000000000000000000000000000111111111111111111111
0000000000000000000000000000000000000000001111111111111111111111
0000000000000000000000000000000000000000011111111111111111111111
0000000000000000000000000000000000000000111111111111111111111111
0000000000000000000000000000000000000001111111111111111111111111
0000000000000000000000000000000000000011111111111111111111111111
0000000000000000000000000000000000000111111111111111111111111111
0000000000000000000000000000000000001111111111111111111111111111
0000000000000000000000000000000000011111111111111111111111111111
0000000000000000000000000000000000111111111111111111111111111111
0000000000000000000000000000000001111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
Upvotes: 2
Views: 153
Reputation:
From 5.8 Shift operators
The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand. The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.
The trouble starts when i becomes 32. In other words int32_t(1) << 32 is undefined behavior.
Upvotes: 1
Reputation: 138051
This is because 1 is treated as an integer and as such, when it is extended to a 64-bit integer, it is sign-extended. Unsigned integers are zero-extended instead.
These are two different behavior of integral extension. Integral extension is what happens when an integral type is transformed into a larger integral type: the computer obviously can't just make the new bits random, so instead compilers pick one of two consistent behaviors.
int
and make it a long
, it'll still be the same number with the same sign. This is what typically happens when you extend signed integers.You'll also need to tell the compiler that 1 is at least as big as your bits
variable, because otherwise the 1 is going to "fall off" when you shift: you're shifting it further than the size of an int, so it will be discarded. You can do that by either casting 1
to ullong
((ullong)1
), or, more concisely, to use the ull
prefix: 1ull
.
Upvotes: 3