borchero
borchero

Reputation: 6002

bit shifting with unsigned long type produces wrong results

I'm a bit confused because I wanted to initialize a variable of type unsigned long whose size is 8 bytes on my system (on every modern system I suppose). When I want to assign 1 << 63 to the variable, I get a compiler warning however and the number is in fact 0. When I do 1 << 30 I get the expected result of 2 ^ 30 = 1073741824. Yet when I do 1 << 31, I get the result of 2 ^ 64 (I think; actually this shouldn't be possible) which prints 18446744071562067968.

Can anyone explain this behaviour to me?

Upvotes: 11

Views: 14164

Answers (4)

P45 Imminent
P45 Imminent

Reputation: 8591

1 << 63 will be computed in int arithmetic, and your int is probably 32 bit.

Remedy this by promoting the left argument: 1ULL << 63 will do it.

ULL means the expression will be at least 64 bits.

Upvotes: 17

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

The 1 here, is called an integer constant. By the norms specified in the standard, C11, chapter §6.4.4.1 the syntax for the same is

integer-constant:
     decimal-constant integer-suffixopt
     octal-constant integer-suffixopt
      hexadecimal-constant integer-suffixopt

and regarding the Semantics,

The type of an integer constant is the first of the corresponding list in which its value can be represented.

and the table says, if there is no suffix, and the value is representable in the int range it should be considered as int. So, 1 here, is considered an int, which is of generally 4 bytes, or 32 bits, also same in your case.

To explicitly specify the 1 as unsigned long (64) bit, we can use the suffix, like

1UL << 63

should solve your issue.

Please note: unsigned long is not guaranteed to be of 64 bits. unsigned long long is guaranteed to have at least 64 bits. However, as long as you're using the platform where unsigned long is of 64 bits , you should be fine

Upvotes: 3

dwalter
dwalter

Reputation: 7468

I recommend that you use 1ULL since this will give you a 64-bit unsigned integer on 32 and 64 bit architecture. On 32 Bit architecture unsigned long (and therefor UL) is only 32 bit long and will not solve the problem.

1ULL << 63

Upvotes: 2

fuz
fuz

Reputation: 92966

The expression 1 << 63 has type int. The range of an int is -231 … 231 - 1 on most systems, 263 is too large for that. Try either (unsigned long)1 << 63 or 1UL << 63 to shift a value of type unsigned long left by 63 places.

Upvotes: 3

Related Questions