Long Type 64-bit Linux

Very simple questions, guys, but maybe I'm just forgetting something. On 64-bit Linux, long 8bytes is correct? If so, and I want to set the 64th bit, I can do the following:

unsigned long num = 1<<63;

Whenever I compile this, it gives me an error saying that I am left shifted more than the width. Also, if I wanted to take the first 32 bits of a long type (without a character extension), can I do:

num = num&0xFFFFFFFF;

or how about:

num = (int)(num);

Thank.

+5
source share
5 answers

On 64-bit Linux, long 8bytes is correct?

Not necessary. Depends on the compiler than on the base OS. Check it out for a pleasant discussion. What determines the size of an integer?

, , , , ,

. 1UL

, 32 ( ), :

num = num&0xFFFFFFFF;
or what about:

num = (int)(num);

num = num&0xFFFFFFFF. 32 . , long 4 , . , long, unsigned long, . , -1 , 0- . ?

num = (int)(num) 32-, , num int

+3

, ( ) , C99, #include <stdint.h> , int64_t, int32_t .. intptr_t, , void* ( "" )

+4

:

limits.h

#define LNG_BIT   (sizeof(long) * CHAR_BIT)

unsigned long num = 1UL << (LNG_BIT - 1);

" int", - ?:

#define INT_BIT   (sizeof(int) * CHAR_BIT)

if (LNG_BIT > INT_BIT)
    return num & (~0UL >> INT_BIT);
else
    return num;

    num &= ~(~0U << INT_BIT);

, .. , , .. int.

, ; gcc:

-m32
-m64
-mx32
    32- 64- .
     * -m32 int, long pointer 32 , i386.
     * -m64 int 32 , - 64 x86-64. -m64 -fno-pic -mdynamic-no-pic.
     * -mx32 int, long pointer 32 x86-64. >

-maddress-mode=long ..

-maddress-mode = long
    . 64- x32-. 64- . >

+1

, , "1" , . , .

, :

unsigned long num = (unsigned long)1<<63;
0

AFAIR, , reiserfs :

unsigned long num = 1<<63;

x86_64, , 64 64- . , 1, 63 int, undefined.

 unsigned long num = 1UL<<63;

 unsigned long num = (unsigned long)1<<63;
0

All Articles