Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(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

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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';
}

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml