A bit shift example : Bit Shift « Data Type « C / ANSI-C






A bit shift example

A bit shift example

#include <stdio.h>

int main(void)
{
  unsigned int i;
  int j;

  i = 1;

  /* left shift i by 1, which is same as a multiply by 2 */

  for(j = 0; j < 6; j++) {
    i = i << 1;  
    printf("Left shift %d: %d\n", j, i);
  }

  /* right shift i by 1, which is same as a division by 2 */
 
  for(j = 0; j < 4; j++) {
    i = i >> 1;      
    printf("Right shift %d: %d\n", j, i);
  }

  return 0;
}
  

           
       








Related examples in the same category

1.Shift bitwise and display the resultShift bitwise and display the result
2.Use bit shift to calculate
3.Shift statement in for