Declaring 64-bit variables in C

后端 未结 5 1925
生来不讨喜
生来不讨喜 2020-12-01 17:41

I have a question.

uint64_t var = 1; // this is 000000...00001 right?

And in my code this works:

var ^ (1 << 43)
         


        
5条回答
  •  不知归路
    2020-12-01 18:28

    A portable way to have a unit64_t constant is to use UINT64_C macro (from stdint.h):

    UINT64_C(1) << 43
    

    Most likely UINT64_C(c) is defined to something like c ## ULL.

    From the C standard:

    The macro INTN_C(value) shall expand to an integer constant expression corresponding to the type int_leastN_t. The macro UINTN_C(value) shall expand to an integer constant expression corresponding to the type uint_leastN_t. For example, if uint_least64_t is a name for the type unsigned long long int, then UINT64_C(0x123) might expand to the integer constant 0x123ULL.

提交回复
热议问题