Skip to content

Instantly share code, notes, and snippets.

@calvinfroedge
Created February 20, 2012 03:24
Show Gist options
  • Save calvinfroedge/1867570 to your computer and use it in GitHub Desktop.
Save calvinfroedge/1867570 to your computer and use it in GitHub Desktop.
Shift operators
#include "stdio.h"
int main()
{
int x, z, y = 2, a = 3, r;
x = y << 2; //Binary operation. Bits are shifted two places to the left (001 becomes 100). This evalutes to 8
printf("%d \n \n", x);
z = a << 2; //Binary operation. a starts as 011 and bits are shifted two spaces to the left. a becomes 1100 (12)
printf("%d \n \n", z);
r = z >> 2; //Binary operation. r shifts z to spaces right. r is 3.
printf("%d \n \n", r);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment