Skip to content

Instantly share code, notes, and snippets.

@kobake
Last active February 3, 2017 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobake/f28b3e92a37ba116cb988b2fc7b6c8ff to your computer and use it in GitHub Desktop.
Save kobake/f28b3e92a37ba116cb988b2fc7b6c8ff to your computer and use it in GitHub Desktop.
Bitwise shift operators: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
#include <stdio.h>
int main() {
int n = 1;
for (int i = 0; i < 32; i++) {
n <<= 1;
}
printf("%d\n", n); // gcc: 0 msvc: 0
int m = 1;
m <<= 32;
printf("%d\n", m); // gcc: 1 msvc: 1 (the behavior is undefined)
long long lln = 1;
for (int i = 0; i < 64; i++) {
lln <<= 1;
}
printf("%lld\n", lln); // gcc: 0 msvc: 0
long long llm = 1;
llm <<= 64;
printf("%lld\n", llm); // gcc: 1 msvc: 0 (the behavior is undefined)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment