Long type 64bit linux

bit-manipulation, c, linux

Solution

In 64bit linux, a long is 8bytes correct?

Need not be. Depends on the compiler than on the underlying OS. Check this for a nice discussion. What decides the sizeof an integer?

Whenever I compile this, however, it gives me an error saying that I'm left shifting by more than the width

Everyone have already answered this. Use `1UL`

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);

`num = num&0xFFFFFFFF`. This will give you the lower 32-bits. But note that if `long` is just 4 bytes on your system then you are getting the entire number. Coming to the sign extension part, if you've used a `long` and not `unsigned long` then you cannot do away with the sign extended bits. For example, `-1` is 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 if `num` does not fit into an `int`

Problem

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.

Original source

Related problems