Search code examples
c++bit-manipulation64-bitbitmask

How to create a number with (f)16 repeating n times?


I need to create a number where (f)16 repeats n times. 0 < n <= 16.

I tried the following for example for n = 16

std::cout << "hi:" << std::hex << std::showbase << (1ULL << 64) - 1 << std::endl;

warning: shift count >= width of type [-Wshift-count-overflow] std::cout << "hi:" << std::hex << std::showbase << (1ULL << 64) - 1 << std::endl; ^ ~~ 1 warning generated.

hi:0x200

How can I get all digits f without overflowing ULL ?


Solution

  • For n = 1 to 16, you could start with all Fs and then shift accordingly:

    0xFFFFFFFFFFFFFFFFULL >> (4*(16-n));
    

    (handle n=0 separately)