C# Shift Operators

This C# article shows the shift operators. Shift moves all the bits right or left.

Shift moves bit positions.

It changes the bit representation of a type. The bits are shifted right (or left) a number of positions. The C# language enables bitwise shifting by offering the right shift (>>) and left shift (<<) operators.

 

 

Example. We introduce a program that shows the right shift (>>) and then left shift (<<) bitwise operators in the C# language. We repeatedly apply them and change the value of a random integer of an int type.

 

Note: The output of the program illustrates how the bit values are changed with the parameters to the shift operators.

Note 2: The output also shows the decimal value of the integer is influenced by these shifts.

C# program that uses right shift and left shift

using System;

class Program
{
    static void Main()
    {
	// This program takes a random integer and then shifts it right.
	// ... Then it shifts it left.
	// ... It displays the bits and also the decimal representation.
	int value1 = new Random().Next();
	for (int i = 0; i < 32; i++)
	{
	    int shift = value1 >> i;
	    Console.WriteLine("{0} = {1}",
		GetIntBinaryString(shift), shift);
	}
	for (int i = 0; i < 32; i++)
	{
	    int shift = value1 << i;
	    Console.WriteLine("{0} = {1}",
		GetIntBinaryString(shift), shift);
	}
    }

    static string GetIntBinaryString(int n)
    {
	// Use method body from:
	// ... binary-representation
    }
}

Output

01001010000010100110001100110111 = 1242194743
00100101000001010011000110011011 = 621097371
00010010100000101001100011001101 = 310548685
00001001010000010100110001100110 = 155274342
00000100101000001010011000110011 = 77637171
00000010010100000101001100011001 = 38818585
00000001001010000010100110001100 = 19409292
00000000100101000001010011000110 = 9704646
00000000010010100000101001100011 = 4852323
00000000001001010000010100110001 = 2426161
00000000000100101000001010011000 = 1213080
00000000000010010100000101001100 = 606540
00000000000001001010000010100110 = 303270
00000000000000100101000001010011 = 151635
00000000000000010010100000101001 = 75817
00000000000000001001010000010100 = 37908
00000000000000000100101000001010 = 18954
00000000000000000010010100000101 = 9477
00000000000000000001001010000010 = 4738
00000000000000000000100101000001 = 2369
00000000000000000000010010100000 = 1184
00000000000000000000001001010000 = 592
00000000000000000000000100101000 = 296
00000000000000000000000010010100 = 148
00000000000000000000000001001010 = 74
00000000000000000000000000100101 = 37
00000000000000000000000000010010 = 18
00000000000000000000000000001001 = 9
00000000000000000000000000000100 = 4
00000000000000000000000000000010 = 2
00000000000000000000000000000001 = 1
00000000000000000000000000000000 = 0
01001010000010100110001100110111 = 1242194743
10010100000101001100011001101110 = -1810577810
00101000001010011000110011011100 = 673811676
01010000010100110001100110111000 = 1347623352
10100000101001100011001101110000 = -1599720592
01000001010011000110011011100000 = 1095526112
10000010100110001100110111000000 = -2103915072
00000101001100011001101110000000 = 87137152
00001010011000110011011100000000 = 174274304
00010100110001100110111000000000 = 348548608
00101001100011001101110000000000 = 697097216
01010011000110011011100000000000 = 1394194432
10100110001100110111000000000000 = -1506578432
01001100011001101110000000000000 = 1281810432
10011000110011011100000000000000 = -1731346432
00110001100110111000000000000000 = 832274432
01100011001101110000000000000000 = 1664548864
11000110011011100000000000000000 = -965869568
10001100110111000000000000000000 = -1931739136
00011001101110000000000000000000 = 431489024
00110011011100000000000000000000 = 862978048
01100110111000000000000000000000 = 1725956096
11001101110000000000000000000000 = -843055104
10011011100000000000000000000000 = -1686110208
00110111000000000000000000000000 = 922746880
01101110000000000000000000000000 = 1845493760
11011100000000000000000000000000 = -603979776
10111000000000000000000000000000 = -1207959552
01110000000000000000000000000000 = 1879048192
11100000000000000000000000000000 = -536870912
11000000000000000000000000000000 = -1073741824
10000000000000000000000000000000 = -2147483648

This program introduces the Program class. It contains the Main entry point, where control flow begins. The Main method body first acquires a random integer, which can be negative or positive. This uses the Random number generator class.

Random

Next, the program uses a loop. In it the random integer is shifted right (>>) zero places, then one place, and then two and more places. The console window is filled with the output of these shifts.

And: The following loop repeats this, but pushes the bits to the left by using the left shift (<<) operator.

Note: The left shift changes the decimal value of the output because the sign bit is changed by this operation.

Also: You can see that all numbers that are negative have the very leftmost (first) bit set to 1, while positive numbers have it set to 0.

 

Uses. Each time you use the Dictionary or Hashtable collection or call the GetHashCode method on a string, shift operators are used to acquire the hash code. You can use a right shift to implement divide by two on positive integral types.

 

Tip: If you are using a data structure that uses bitmasks, bit shifting can be used to guide the control flow.

GetHashCodeDivide by Powers of Two

 

Binary operators. The term "binary operators" is used to describe operators that receive two parameters. The term "bitwise operator" indicates an operator that receives one or two operands and is meant to change the bit representation of an integer.

 

The term "unary operator" is used to indicate an operator such as unary negation that receives only one argument. It is the opposite of "binary operator" and not bitwise operator.

 

Summary. We explored the shift operator in the C# language and how it can be applied to change the value of an integer. The article provides a program to illustrate this clearly through a simulation with a random number.

 

Review: We touched on the sign bit. We then noted other uses of the shift operators in the .NET Framework.