3
int value = 0xffffffff;
int len = 32;
int result = value << len; // result will be 0xffffffff
result = value << 32; // result will be 0x0

Why does it makes a difference?

Edit: Sorry I made a mistake. In the example above, both results are 0xffffffff. So look at this:

unsigned int value = 0xffffffff;
unsigned int len = 32;
printf("0x%x\n", value << len); //will print 0xffffffff
printf("0x%x\n", 0xffffffff << 32); //will print 0x0
  • 1
    what compiler are you using? The problem is probably caused by the fact that value is signed, but not sure. I got both result zero: http://ideone.com/XdBAyb – Ryzhehvost Apr 02 '14 at 11:22
  • @Ryzhehvost There's no reason not to expect differences between compilers, since unless `int` is larger than 32 bits, the behavior is undefined. – James Kanze Apr 02 '14 at 11:34
  • Re your edit: undefined behavior is, well, undefined, so nothing should surprise you. – James Kanze Apr 02 '14 at 11:37
  • @James, you are right. It's undefined, because int is signed. – Ryzhehvost Apr 02 '14 at 11:38
  • @Ryzhehvost The signedness is irrelevant. (When the signedness comes into play is when right-shifting, and it doesn't introduce undefined behavior, just implementation defined behavior.) – James Kanze Apr 02 '14 at 11:46
  • @James, I didn't knew that. I had only read about undefined behavior with signed int (on msdn). Will remember this, thank you. – Ryzhehvost Apr 02 '14 at 11:52

2 Answers2

3

If len >= sizeof(int) or len < 0, the code contains undefined behaviour.

See this answer for more details.

Community
  • 1
  • 1
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
  • Thats what I want and what I would expect but in fact they are both 0xffffffff (VS2010) – user3249723 Apr 02 '14 at 11:30
  • The code has undefined behavior, so you can't say anything about the results. – James Kanze Apr 02 '14 at 11:35
  • @JamesKanze Can you enlighten me on this? – HelloWorld123456789 Apr 02 '14 at 11:37
  • @RikayanBandyopadhyay See my response. Shifting a 32 bit int 32 or more bits is undefined behavior. – James Kanze Apr 02 '14 at 11:38
  • James is correct here, shifts are undefined if the shift amount is negative or greater or equal to the bit count of the promoted type, see the quote in [my answer here](http://stackoverflow.com/questions/18918256/is-right-shift-undefined-behavior-if-the-count-is-larger-than-the-width-of-the-t/18918340#18918340). – Shafik Yaghmour Apr 02 '14 at 11:41
3

If the size of an int is 32 bits or less, your code contains undefined behavior. The number of bits shifted must be greater than or equal 0, and strictly less than the number of bits in what is being shifted.

What is probably happening in practice is that for the variable, the compiler is probably just passing it to a machine instruction which only considers 5 low order bits (which are 0 in the case of 32); when the shift count is a constant, the compiler evaluates the expression internally, likely in long long, and then truncates it. But this is just one possible behavior; anything might happen as far as the language is concerned.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Ok that would justify why ideone.com says 0 and VS2010 says -1 – user3249723 Apr 02 '14 at 11:34
  • @user3249723 Quite. (While undefined, I suspect that most compilers with 32 bit ints will give one of these two results.) – James Kanze Apr 02 '14 at 11:36
  • @presiuslitelsnoflek I keep completing the answer:-). I've made the correction you did now myself. I probably accidentally hit `.`, which repeats the last command in my editor (and the last command was the insertion of the first sentence). – James Kanze Apr 02 '14 at 11:40