bit-shift doesn't produce expected 0!

F

fizzleBomb

After running this:

unsigned long long x = (unsigned long long)-1;
int y = 64;
x = x >> y;

the value of x is 0xffffffffffffffff though I'd have expected 0. Strangely,
shifting by 63 produces 1 as expected. Any idea what's up with that?

I'm using gcc 4.0.2 if that's relevant.
 
K

Kai-Uwe Bux

fizzleBomb said:
After running this:

unsigned long long x = (unsigned long long)-1;
int y = 64;
x = x >> y;

the value of x is 0xffffffffffffffff though I'd have expected 0.
Strangely, shifting by 63 produces 1 as expected. Any idea what's up with
that?

Undefined behavior [5.8/1]:

The shift operators << and >> group left-to-right.
[...]
The operands shall be of integral or enumeration type and integral
promotions are performed. The type of the result is that of the promoted
left operand. The behavior is undefined if the right operand is negative,
or greater than or equal to the length in bits of the promoted left
operand.
I'm using gcc 4.0.2 if that's relevant.

It isn't.


Best

Kai-Uwe Bux
 
F

fizzleBomb

Kai-Uwe Bux said:
Undefined behavior [5.8/1]:

The shift operators << and >> group left-to-right.
[...]
The operands shall be of integral or enumeration type and integral
promotions are performed. The type of the result is that of the promoted
left operand. The behavior is undefined if the right operand is
negative, or greater than or equal to the length in bits of the promoted
left operand.
I'm using gcc 4.0.2 if that's relevant.

It isn't.


Best

Kai-Uwe Bux

Thanks much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top