Skip to content

Instantly share code, notes, and snippets.

@scvalex
Created May 11, 2013 01:47
Show Gist options
  • Save scvalex/5558601 to your computer and use it in GitHub Desktop.
Save scvalex/5558601 to your computer and use it in GitHub Desktop.
An example to illustrate the surprising precedence of `<<` in action.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("1 << 1 + 2 = %d\n", 1 << 1 + 2);
printf("1 << 1 * 3 = %d\n", 1 << 1 * 3);
printf("1 << 1 + 1 << 1 = %d\n", 1 << 1 + 1 << 1);
return 0;
}
@scvalex
Copy link
Author

scvalex commented May 11, 2013

Build and run:

% gcc -Wall -o prec prec.c && ./prec
prec.c: In function ‘main’:
prec.c:4:5: warning: suggest parentheses around ‘+’ inside ‘<<’ [-Wparentheses]
prec.c:6:5: warning: suggest parentheses around ‘+’ inside ‘<<’ [-Wparentheses]
1 << 1 + 2      = 8
1 << 1 * 3      = 8
1 << 1 + 1 << 1 = 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment