(C++) left shift count >= width of type
February 24, 2017 · View on GitHub
(C++) left shift count >= width of type
left shift count >= width of type is a compile warning when bit shifting too much up:
#include <cinttypes> int main() { const int64_t i = (1 << 63); //Gives warning }
The code shown, however, might give this compile warning unexpectedly. Sure, a 1 shifted 63 times up will fit in a 64-bit int. But the 1 itself must be 64 bit as well:
#include <cinttypes> int main() { const int64_t i = (1LL << 63); }
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 12.10 (quantal)
Qt Creator 2.5.2
- G++ 4.7.2
Libraries used:
STL: GNU ISO C++ Library, version
4.7.2
Qt project file: CppCompileWarningLeftShiftCounterBiggerOrEqualToWidthOfType.pro
TEMPLATE = app QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror CONFIG += console CONFIG -= qt SOURCES += main.cpp
main.cpp
#include <cinttypes> #include <iostream> int main() { //const int64_t i = (1 << 63); //Gives warning const int64_t i = (1LL << 63); std::cout << i << '\n'; }