Mega Code Archive

 
Categories / C / Code Snippets
 

A bit shift

#include <stdio.h> int main(void) { unsigned int i; int a; i = 1; /* left shift i by 1, which is same as a multiply by 2 */ for(a = 0; a < 6; a++) { i = i << 1; printf("Left shift %d: %d\n", a, i); } /* right shift i by 1, which is same as a division by 2 */ for(a = 0; a < 4; a++) { i = i >> 1; printf("Right shift %d: %d\n", a, i); } return 0; }