Very simple questions guys, but maybe I’m just forgetting something.
In 64bit linux, a long is 8bytes correct?
If that’s the case, and I want to set the 64th bit, I can do the following:
unsigned long num = 1<<63;
Whenever I compile this, however, it gives me an error saying that I’m left shifting by more than the width.
Also, if I wanted to take the first 32bits of a long type (without sign extension), can I do:
num = num&0xFFFFFFFF;
or what about:
num = (int)(num);
Thank you.
Need not be. Depends on the compiler than on the underlying OS. Check this for a nice discussion.
What decides the sizeof an integer?
Everyone have already answered this. Use
1ULnum = num&0xFFFFFFFF. This will give you the lower 32-bits. But note that iflongis just 4 bytes on your system then you are getting the entire number. Coming to the sign extension part, if you’ve used alongand notunsigned longthen you cannot do away with the sign extended bits. For example,-1is represented as all ones, right from the 0th bit. How will you avoid these ones by masking?num = (int)(num)will give you the lower 32-bits but compiler might through a Overflow Exception warning ifnumdoes not fit into anint