Limits of << operation


Hi all,

Assuming unsigned ints are 32 bits in size, I think it is obvious [is it?] that this is bad:

unsigned int x = (1 << 32);

But is this no different and also bad:

unsigned int x = ((1 << 32) - 1);

?? Since the temporary value is outside the range of unsigned ints. As you can tell, I want x to have all 1's. #define a constant seems safer, but I'm just wondering if the above is a bad idea.

I have been using it for some time and it seems to be ok, but I'm wondering if I've just been lucky and the compilers I've been using have just been accepting of what I'm doing...

Thank you!

Ray

The 1<<32 is quite superfluous, as it will evaluate to 0.
So
1
2
uint x=-1; //or better yet:
uint y=0xFFFFFFFFu;

does the trick.
Last edited on
or better yet:

uint y = ~0;

~ is the complement operator, it toggles all bits. so ~0 has all bits set.
1
2
3
#include <limits>

uint y = std::numeric_limits<uint>::max();


Hi all,

Thank you all for your replies! What I was doing was useless, as I had suspected... And thank you for giving me 4 options -- none of which I had thought about. I'll be sure to give them a try; thanks!

Ray


Topic archived. No new replies allowed.