shift count overflow when doing 1<<40

Viewed 1681

I am puzzled on the following code behavior as to left shift: the intent is to define a variable terabytes, TB = 1024*1024*1024*1024, which would be 2**40 or 1<<40. What is it complaining:

warning: shift count >= width of type [-Wshift-count-overflow]
    uint64_t TB = 1<<40;
                   ^ ~~
1 warning generated.

This is compiled on Mac (10.13.1), with clang-900.0.38, -std=c++11.

#include <cstdint>
#include <iostream>

int main(int argc, char** argv) {

    uint64_t TB = 1<<40;
    std::cout << "TB = " << TB << std::endl;
    return 0;
}
1 Answers

Try this code :

1ULL << 40

Constants need a qualified type if you want something other than default.

Related