How to shift >= 32 bits in uint64_t?

后端 未结 1 370
说谎
说谎 2021-01-21 04:16

The following code triggers a gcc warning (gcc 4.2.1):

#include 
boost::uint64_t x = 1 << 32; // warning: left shift          


        
相关标签:
1条回答
  • 2021-01-21 04:36

    How to shift >= 32 bits in uint64_t?

    If your compiler supports long long:

    boost::uint64_t x = 1LL << 32;
    

    Otherwise:

    boost::uint64_t x = boost::uint64_t(1) << 32;
    

    Shouldn't it be fine since the type has 64 bits?

    No. Even though x is 64 bits, 1 isn't. 1 is 32 bits. How you use a result has no effect on how that result is generated.

    0 讨论(0)
提交回复
热议问题