Using shift operators

Tuesday, October 14, 2014
/* 0806.c: Using shift operators */
/*
The operation of the shift-right operator (>>) is equivalent to dividing by powers of 2. In other words,
the following:
x >> y
x / 2y
Here x is a non-negative integer.
On the other hand, shifting to the left is equivalent to multiplying by powers of 2; that is,
x << y
x * 2y
*/

#include <stdio.h>

main()
{
    int x, y, z;

    x = 255;
    y = 5;
    printf("Given x = %4d, i.e., 0X%04X\n", x, x);
    printf(" y = %4d, i.e., 0X%04X\n", y, y);
    z = x >> y;
    printf("x >> y returns: %6d, i.e., 0X%04X\n", z, z);
    z = x << y;
    printf("x << y returns: %6d, i.e., 0X%04X\n", z, z);
    return 0;
}

Copyright @ 2015 Tron!

Labels

Blog Archive