Is static_cast(-1) the right way to generate all-one-bits data without numeric_limits?

后端 未结 5 1686
温柔的废话
温柔的废话 2021-02-05 03:28

I\'m writing C++ code in an environment in which I don\'t have access to the C++ standard library, specifically not to std::numeric_limits. Suppose I want to implem

相关标签:
5条回答
  • 2021-02-05 03:44

    Another way is

    static_cast<T>(-1ull)
    

    which would be more correct and works in any signed integer format, regardless of 1's complement, 2's complement or sign-magnitude. You can also use static_cast<T>(-UINTMAX_C(1))

    Because unary minus of an unsigned value is defined as

    The negative of an unsigned quantity is computed by subtracting its value from 2^n, where n is the number of bits in the promoted operand."

    Therefore -1u will always return an all-one-bits data in unsigned int. ll suffix is to make it work for any types narrower than unsigned long long. There's no extended integer types (yet) in C++ so this should be fine

    However a solution that expresses the intention clearer would be

    static_cast<T>(~0ull)
    
    0 讨论(0)
  • 2021-02-05 03:49

    This disarmingly direct way.

    T allOnes;
    memset(&allOnes, ~0, sizeof(T));
    
    0 讨论(0)
  • 2021-02-05 03:58

    Focusing on unsigned integral types, what do I put there? Specifically, is static_cast(-1) good enough

    Yes, it is good enough.

    But I prefer a hex value because my background is embedded systems, and I have always had to know the sizeof(T).

    Even in desktop systems, we know the sizes of the following T:

    uint8_t  allones8  = 0xff;
    uint16_t allones16 = 0xffff;
    uint32_t allones32 = 0xffffffff;
    uint64_t allones64 = 0xffffffffffffffff;
    
    0 讨论(0)
  • 2021-02-05 04:04

    Focusing on unsigned integral types, what do I put there? Specifically, is static_cast(-1) good enough

    If you're only concerned about unsigned types, yes, converting -1 is correct for all standard C++ implementations. Operations on unsigned types, including conversions of signed types to unsigned types, are guaranteed to work modulo (max+1).

    0 讨论(0)
  • 2021-02-05 04:08

    Use the bitwise NOT operator ~ on 0.

    T allOnes = ~(T)0;
    

    A static_cast<T>(-1) assumes two's complement, which is not portable. If you are only concerned about unsigned types, hvd's answer is the way to go.

    Working example: https://ideone.com/iV28u0

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