sat.learner
sat.learner

Reputation: 3

Shifting on Integer Constants shows warning. How to clear this?

Reference: Suffix in Integer Constants

unsigned long long y = 1 << 33;

Results in warning:

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

Two Questions need to be cleared from the above context:

  1. unsigned long long type has 64-bit, why cant we do left shift in it?
  2. how shifting works in int constants('1')?

Upvotes: 0

Views: 119

Answers (1)

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29985

In C language, 1 is an int which is 32 bits on most platforms. When you try to shift it 33 bits before storing its value in an unsigned long long, that's not going to end well. You can fix this in 2 ways:

  • Use 1ULL instead, which is an unsigned long long constant:
unsigned long long y = 1ULL << 33;
  • Assign the value, then shift it:
unsigned long long y = 1;
y <<= 33;

Both are valid, but I'd suggest the first one since it's shorter and you can make y const.

Upvotes: 3

Related Questions