How to Shift Binary Values in C Programming

By Dan Gookin

The C programming language features two binary operators that perform the equivalent operation of “Everyone move one step to the left (or right).” The << and >> operators shift bits in value, marching them to the left or right, respectively. Here’s the format for the << operator:

v = int << count;

int is an integer value. count is the number of places to shift the value’s bits to the left. The result of this operation is stored in variable v. Any bits that are shifted to the left beyond the width of the int variable x are lost. New bits shifted in from the right are always 0.

As with most binary nonsense, it helps to visually see what’s going on in a value when its bits are shifted.

EVERYONE OUT OF THE POOL!

#include <stdio.h>
char *binbin(int n);
int main()
{
 int bshift,x;
 printf("Type a value from 0 to 255: ");
 scanf("%d",&bshift);
 for(x=0;x<8;x++)
 {
 printf("%sn",binbin(bshift));
 bshift = bshift << 1;
 }
 return(0);
}
char *binbin(int n)
{
 static char bin[9];
 int x;
 for(x=0;x<8;x++)
 {
 bin[x] = n & 0x80 ? '1' : '0';
 n <<= 1;
 }
 bin[x] = '';
 return(bin);
}

The shift operation takes place at Line 15 in Everyone out of the Pool!. The value in variable bshift is shifted to the left one bit.

Exercise 1: Type the source code from Everyone out of the Pool! into your editor and build a new project.

The net effect of a left bit shift is to double a value. That holds true to a certain point: Obviously, the farther left you shift, some bits get lost and the value ceases to double. Also, this trick works only for unsigned values.

Exercise 2: Modify the source code from Everyone out of the Pool! so that the printf() function at Line 14 also displays the decimal value of the bshift variable. You should also modify the binbin() function so that it displays 16 digits instead of 8.

Here’s the output when using the value 12:

Type a value from 0 to 255: 12
0000000000001100 12
0000000000011000 24
0000000000110000 48
0000000001100000 96
0000000011000000 192
0000000110000000 384
0000001100000000 768
0000011000000000 1536

Try the value 800,000,000 (don’t type the commas) to see how the doubling rule fails as the values keep shifting to the left. Also see the nearby sidebar “Negative binary numbers.”

The >> shift operator works similarly to the << shift operator, though values are marched to the right instead of the left. Any bit that’s marched off the right end is discarded, and only zero bits are inserted on the left side. Here’s the format:

v = int >> count;

int is an integer value, and count is the number of places to shift the bits to the right. The result is stored in variable v.

Exercise 3: Modify the source code from Exercise 2 so that the right shift operator is used instead of the left shift at Line 15. Build the program.

Here’s the result when using the value 128:

Type a value from 0 to 255: 128
0000000010000000 128
0000000001000000 64
0000000000100000 32
0000000000010000 16
0000000000001000 8
0000000000000100 4
0000000000000010 2
0000000000000001 1

Unlike the << operator, the >> is guaranteed to always cut the value in half when you shift one digit to the right. In fact, the >> operator is far quicker to use on an integer value than the / (division) operator to divide a value by 2.

The << and >> operators are available only in the C language. In C++, similar operators are used to receive standard input and send standard output.

Binary numbers are always positive, considering that the values of a bit can be only 1 or 0 and not –1 and 0. So how does the computer do signed integers? Easy: It cheats.

The leftmost bit in a signed binary value is known as the sign bit. When that bit is set (equal to 1), the value is negative for a signed int. Otherwise, the value is read as positive.

image0.jpg

In this example, the sign bit is set for a signed char. The values expressed are negative, which is in the range of a signed char variable.

image1.jpg

In this example, the sign bit is ignored because the value is an unsigned char. The values can only be positive, which is why the positive range for an unsigned variable is greater than for a signed variable.