Home
Map
Bitwise Shift OperatorsUse the shift operators. A shift moves all the bits right or left.
C#
This page was last reviewed on Jul 18, 2021.
Shift. In C# this operator moves bit positions. It changes the bit representation of a type. The bits are shifted right (or left) a number of positions.
Operator notes. The C# language enables bitwise shifting with the right and left shift operators. With these operators, individual bits are all moved together.
Binary Representation
Bitwise
Bitwise XOR
Input and output. Consider a bit pattern that is part of an integer. We shift to the right several times (the arrows point in the shifting direction).
101 >> 1: 010 >> 1: 001 >> 1: 000
An 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 an int.
Info The integer is shifted right zero places, then one place, and then two and more places.
Tip 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.
Note The output of the program illustrates how the bit values are changed with the parameters to the shift operators.
using System; class Program { static void Main() { // This program shift an integer right. // ... Then it shifts it left. // ... It displays the bits and the decimal representation. int value1 = 99999999; 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 value) { // From other article. return Convert.ToString(value, 2).PadLeft(32, '0'); } }
00000101111101011110000011111111 = 99999999 00000010111110101111000001111111 = 49999999 00000001011111010111100000111111 = 24999999 00000000101111101011110000011111 = 12499999 00000000010111110101111000001111 = 6249999 00000000001011111010111100000111 = 3124999 00000000000101111101011110000011 = 1562499 00000000000010111110101111000001 = 781249 00000000000001011111010111100000 = 390624 00000000000000101111101011110000 = 195312 00000000000000010111110101111000 = 97656 00000000000000001011111010111100 = 48828 00000000000000000101111101011110 = 24414 00000000000000000010111110101111 = 12207 00000000000000000001011111010111 = 6103 00000000000000000000101111101011 = 3051 00000000000000000000010111110101 = 1525 00000000000000000000001011111010 = 762 00000000000000000000000101111101 = 381 00000000000000000000000010111110 = 190 00000000000000000000000001011111 = 95 00000000000000000000000000101111 = 47 00000000000000000000000000010111 = 23 00000000000000000000000000001011 = 11 00000000000000000000000000000101 = 5 00000000000000000000000000000010 = 2 00000000000000000000000000000001 = 1 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000101111101011110000011111111 = 99999999 00001011111010111100000111111110 = 199999998 00010111110101111000001111111100 = 399999996 00101111101011110000011111111000 = 799999992 01011111010111100000111111110000 = 1599999984 10111110101111000001111111100000 = -1094967328 01111101011110000011111111000000 = 2105032640 11111010111100000111111110000000 = -84902016 11110101111000001111111100000000 = -169804032 11101011110000011111111000000000 = -339608064 11010111100000111111110000000000 = -679216128 10101111000001111111100000000000 = -1358432256 01011110000011111111000000000000 = 1578102784 10111100000111111110000000000000 = -1138761728 01111000001111111100000000000000 = 2017443840 11110000011111111000000000000000 = -260079616 11100000111111110000000000000000 = -520159232 11000001111111100000000000000000 = -1040318464 10000011111111000000000000000000 = -2080636928 00000111111110000000000000000000 = 133693440 00001111111100000000000000000000 = 267386880 00011111111000000000000000000000 = 534773760 00111111110000000000000000000000 = 1069547520 01111111100000000000000000000000 = 2139095040 11111111000000000000000000000000 = -16777216 11111110000000000000000000000000 = -33554432 11111100000000000000000000000000 = -67108864 11111000000000000000000000000000 = -134217728 11110000000000000000000000000000 = -268435456 11100000000000000000000000000000 = -536870912 11000000000000000000000000000000 = -1073741824 10000000000000000000000000000000 = -2147483648
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.
Tip You can use a right shift to implement divide by two on positive integral types.
Tip 2 If you are using a data structure that uses bitmasks, bit shifting can be used to guide the control flow.
GetHashCode
Bitwise Divide
Terms. The term "binary operators" is used to describe operators that receive two parameters. A "unary operator" receives only one argument.
Detail The term "bitwise operator" indicates an operator that receives one or two operands. It changes the bit representation.
Summary. We explored the shift operator 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jul 18, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.