Getting Shifty

Something that you can do in the C language that C++ programmers cannot is employ the shift operators. These operators, << and >>, serve as I/O operators in C++. In C, however, they can be used to manipulate values at the bit level in C, which is part of the C language’s mid-level heritage.

When it comes to peeling information from a byte of storage, nothing beats the C language’s low-level operators. In concert with the bitwise logical operators: |, &, and ^, you can use << and >> to manipulate data at the computer equivalent of the microscopic level. You can also do math.

Back in my early programming days, I was an enthusiastic fan of Assembly language, which allows you to directly program a computer’s processor. Assembly has no functions. It has no operators. Everything you do in an Assembly program — everything — must be coded from scratch. That includes the basic math operations multiplication and division.

All processors handle basic addition, subtraction, and increment/decrement operations. Today’s processors have instructions for multiplication or division, but that wasn’t common back in the early days.

The following code demonstrates how multiplication can be done in the C language without using the * multiplication operator. Just as with programming a processor directly by using Assembly language, the operations performed in this code are all at the bit level:

#include <stdio.h>

int main()
{
    int x,bytwo,byeight,byten;

    printf("Type an integer: ");
    scanf("%d",&x);

    bytwo = x << 1;
    byeight = x << 3;
    byten = bytwo + byeight;

    printf("%d x 10 = %d\n",x,byten);

    return(0);
}

Here’s a sample run:

Type an integer: 489
489 x 10 = 4890

It even works on negative numbers:

Type an integer: -30
-30 x 10 = -300

The left-shift operator << is used twice in the above code, first at Line 10 and then at Line 11.

bytwo = x << 1;

The effect of this code is to double the value in variable x. The bits are shifted one notch to the left, which in binary terms multiplies that value by two. This bit-shift operation is extremely fast because the processor can manipulate data directly in that manner. You could almost say the processor enjoys performing that operation.

byeight = x << 3;

This statement shifts the value in x three notches to the left at the binary level. The effect is to multiply the value by 8. Conveniently, 23 is 8, which is why the value 3 is used.

With the original value x multiplied by 2 and stored in variable bytwo, and the original value of x multiplied by 8 and stored in the variable byeight, the result of bytwo and byeight would be to multiply the value x by 10. That’s what the program does. And that’s exactly how the operation would have been performed at the low level in Assembly language.

The only problem you get into is with very large integers, in which case the << operator shifts the left-most bit in a value off into oblivion. But for small values, this trick is quite effective.

2 thoughts on “Getting Shifty

  1. Glenn again for the UK, I want to comment on one thing that’s nearly always missed with bit shift operators, by using them on memory addresses you can change their contents as you said, if that memory address is the location of say a port (Serial port for example, as parallel is rare these days ) if you alter the right address contents you can send a signal out. Bit shifting is one those things that people are not taught about ‘as they don’t need it any more’ (direct quote for a Uni Professor of computing)
    however in the embedded world it get used all the time and people who can program C++ (and get all snuffy about object oriented programming)can come to grief in a spectacular way!

  2. I have an example upcoming where you need a bitshift operator to perform the operation. I’m sure you could do it with the bitwise logical operators, but it would be a pain.

Leave a Reply