Submitted by tushar pramanick on Tue, 03/05/2013 - 21:11

Using Shift Operators

There are two shift operators in C. The >> operator shifts the bits of an operand to the right; the << operator shifts the bits to the left.

The general forms of the two shift operators are

x >> y
x << y

Here x is an operand that is going to be shifted. y contains the specified number of places to shift.

For instance, the 8 >> 2 expression tells the computer to shift 2 bits of the operand 8 to the right, which returns 2 in decimal. The following:

8 >> 2 -> (1 * 23 + 0 *22 + 0 * 21 + 0 *20) >> 2

produces the following:

(0 * 23 + 0 * 22 + 1 *21 + 0 * 20) -> 0010 (binary) -> 2 (decimal).

Likewise, the 5 << 1 expression shifts 1 bit of the operand 5, and yields 10 in decimal.

The program in Listing 8.6 prints out more results by using the shift operators.

TYPE
Listing 8.6. Using the shift operators.


1:  /* 08L06.c: Using shift operators */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int   x, y, z;
7:
8:     x = 255;
9:     y = 5;
10:    printf("Given x = %4d, i.e., 0X%04X\n", x, x);
11:    printf("      y = %4d, i.e., 0X%04X\n", y, y);
12:    z = x >> y;
13:    printf("x >> y  returns: %6d, i.e., 0X%04X\n", z, z);
14:    z = x << y;
15:    printf("x << y  returns: %6d, i.e., 0X%04X\n", z, z);
16:    return 0;
17: }


    OUTPUT
    The following output is obtained by running the executable, 08L06.exe, from a DOS prompt:

    C:\app> 08L06
    Given x =  255, i.e., 0X00FF
          y =    5, i.e., 0X0005
    x >> y  returns:      7, i.e., 0X0007
    x << y  returns:   8160, i.e., 0X1FE0
    C:\app>

    ANALYSIS
    Three integer variables, x, y, and z, are declared in line 6 of Listing 8.6. x is initial-ized with

255 in line 8; y is set to 5 in line 9. Then, lines 10 and 11 display the values of x and y on the screen.

The statement in line 12 shifts y bits of the operand x to the right, and then assigns the result to z. Line 13 prints out the result of the shifting made in line 12. The result is 7 in decimal, or 0X0007 in hex.

Lines 14 and 15 shift the operand x to the left by y bits and display the result on the screen, too. The result of the left-shifting is 8160 in decimal, or 0x1FE0 in hex.

TIP

    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

Related Items

Question and Answer

Question and Answer

    Q Which bit can be used as the sign bit in an integer?

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Measuring Data Sizes

Measuring Data Sizes

What Does x?y:z Mean?

What Does x?y:z Mean?

In C, ?: is called the conditional operator, which is the only operator that takes three operands. The general form of the conditional operator is

Manipulating Bits

Manipulating Bits

In previous hours, you learned that computer data and files are made of bits (or bytes). There is even an operator in C_the sizeof operator_that can be used to measure the number of bytes for data types.