Sumanta
Sumanta

Reputation: 13

Unexpected output when executing left-shift by 32 bits

When I do a left shift of a hex I get -1 as output with the following code:

unsigned int i,j=0;
i= (0xffffffff  << (32-j));
printf("%d",i);

Similarly when I changed the shift value to 32, the output is 0, but I get warnings from compiler as (left shift count >= width of type)

unsigned int i,j=32;
i= (0xffffffff  << (32));
printf("%d",i);

I was expecting the same results in both the cases (ie, 0), but got confused why is displaying wrong output in case #1, and in case #2 the result is correct but the compiler warns!

The result is same in 32 and 64 bit x86 machines.

Can someone explain the results above?

Upvotes: 1

Views: 270

Answers (2)

barak manos
barak manos

Reputation: 30136

Shifting a 32-bit variable by 32 yields undefined behavior.

Here is the assembly generated by the VS-2013 compiler:

    int n = 0;
mov         dword ptr [n],0  
    int a = 0xFFFFFFFF << 32;
mov         dword ptr [a],0  
    int b = 0xFFFFFFFF << (32-n);
mov         ecx,20h  
sub         ecx,dword ptr [n]  
or          eax,0FFFFFFFFh  
shl         eax,cl  
mov         dword ptr [b],eax  

As you can see, what happens de-facto is:

  • When you shift by a constant value of 32, the compiler simply sets the result to 0
  • When you shift by a variable (such as 32-n with n==0), the compiler uses shl

The actual result of shl depends on the implementation of this operation in the underlying architecture. On your processor, it probably takes the 2nd operand modulo 32, hence the 1st operand is shifted by 0.

Again, the description above is not dictated by the standard, so it really depends on the compiler in use.

Upvotes: 2

Yu Hao
Yu Hao

Reputation: 122403

It's undefined behavior to left-shit 32 or greater on a 32-bit integer. That's what the error is about.

C11 6.5.7 Bitwise shift operators

The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

Upvotes: 2

Related Questions