Afficher un message
Vieux 20/10/2007, 12h25   #8
Daniel Kraft
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Inconsistent behaviour for (1 << 32)

>> I did encounter a strange problem in my C program, and traced it down;
>> it looks like I get different results for bit left shifts when the bit
>> count is a constant or a funtion-return value.
>>
>> When doing (1 << 32) for a 32-bit-unsigned I expect to get 0;

>
> The behaviour is undefined if the number of bits by which you are

shifting is >= the number of bits in the object.

Ok, thank you!

What I'm trying to do is this: I've got some unsigned type, and a
function which returns a number of bits <= the number of usable bits of
this type.

I need to calculate (1 << getBits())-1, i.e., set the lower getBits()
bits to one (which might be all ones if it is equal to the type's width,
but it also might be only some less-significant-bits ones).

Is there some other clever way to do this?

Thanks,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
  Réponse avec citation
 
Page generated in 0,04733 seconds with 9 queries