Yongwei Xing
Yongwei Xing

Reputation: 13441

Define the 64 bit width integer in Linux

I try to define a 64 bits width integer using C language on Ubnutu 9.10. 9223372036854775808 is 2^23

long long max=9223372036854775808
long max=9223372036854775808

When I compile it, the compiler gave the warning message:

binary.c:79:19: warning: integer constant is so large that it is unsigned
binary.c: In function ‘bitReversal’:
binary.c:79: warning: this decimal constant is unsigned only in ISO C90
binary.c:79: warning: integer constant is too large for ‘long’ type

Is the long type 64 bits width?

Best Regards,

Upvotes: 2

Views: 27439

Answers (4)

Nicolas Guillaume
Nicolas Guillaume

Reputation: 8434

I'm not sure it's will fix your problem (The LL solution looks good). But here is a recomandation :

You should use hexadecimal notation to write the maximal value of something.

unsigned char max = 0xFF;
unsigned short max = 0xFFFF;
uint32_t max = 0xFFFFFFFF;
uint64_t max = 0xFFFFFFFFFFFFFFFF;

As you can see it's readable.

To display the value :

printf("%lld\n", value_of_64b_int);

Upvotes: 1

caf
caf

Reputation: 239011

The problem you are having is that the number you're using (9223372036854775808) is 2**63, and the maximum value that your long long can hold (as a 64 bit signed 2s complement type) is one less than that - 2**63 - 1, or 9223372036854775807 (that's 63 binary 1s in a row).

Constants that are too large for long long (as in this case) are given type unsigned long long, which is why you get the warning integer constant is so large that it is unsigned.

The long type is at least 32 bits, and the long long type at least 64 bits (both including the sign bit).

Upvotes: 7

kennytm
kennytm

Reputation: 523154

   long long max=9223372036854775808LL; // Note the LL
// long max=9223372036854775808L; // Note the L

A long long type is at least 64 bit, a long type is at least 32 bit. Actual width depends on the compiler and targeting platform.

Use int64_t and int32_t to ensure the 64-/32-bit integer can fit into the variable.

Upvotes: 11

user184968
user184968

Reputation:

I think it depends on what platform you use. If you need to know exactly what kind of integer type you use you should use types from Stdint.h if you have this include file on you system.

Upvotes: 0

Related Questions