Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long type 64bit linux

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.

like image 246
de1337ed Avatar asked Apr 06 '12 06:04

de1337ed


People also ask

Can long be 64-bit?

long , ptr , and off_t are all 64 bits (8 bytes) in size.

What is the range of 64 bits long data type?

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.


1 Answers

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

like image 86
Pavan Manjunath Avatar answered Sep 28 '22 08:09

Pavan Manjunath