2

I am trying to do the following. However I am not sure where I might be going wrong

uint64_t x = (1 << 46); 
std::cout << x;

I get the

-warning: left shift count >= width of type [-Wshift-count-overflow]

I get the output 0. I was expecting something a decimal output of a binary like this

1 0000........00 (46 0s) 

My question is why am I getting this warning ? isnt uint64_t 64 bit ? also why am I getting the output 0 ?

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • 1
    Possible duplicate of [warning: left shift count >= width of type](https://stackoverflow.com/questions/4201301/warning-left-shift-count-width-of-type) – Raymond Chen Aug 26 '18 at 05:10

2 Answers2

6

The problem is that you are not shifting a 64-bit constant: 1 is a constant of type int, which is less than 64 bits on your platform (probably 32 bits; it is implementation-defined).

You can fix this by using UINT64_C macro around the constant:

uint64_t x = (UINT64_C(1) << 46); 
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

1 is a 32-bit constant. The compiler (correctly) computes the constant expression as 0 --- the 1 shifted beyound the size of an int32. If the arguments to << were variables, an x86 cpu would return (1 << 14), i.e. 1 << (46 % 32). Try "1ULL << 46".

Mischa
  • 2,240
  • 20
  • 18