Last active
February 3, 2017 02:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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