Pages

Monday, December 20, 2010

Miscellanea # 1

I've been unpleasantly surprised by very usual thing. I was coding bitwise shift of array of ints and I encountered a certain problem. Let's consider that code:

unsigned a = 0x01234567; //unsigned is 32-bit
unsigned b = a << 32;

What do you think, what it does? I am sure that some people smile at the moment, because they know that the value of b doesn't have to be 0! Yes, that's true at least on the Visual C++ compiler. As we can read on MSDN"The results are undefined if the right operand of a shift expression is negative or if the right operand is greater than or equal to the number of bits in the (promoted) left operand."
So, as you can see even simple operations can be sophisticated...