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.
The C# language enables bitwise shifting with the right and left shift operators. With these operators, individual bits are all moved together.
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
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
.
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
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.
The term "binary operators" is used to describe operators that receive two parameters. A "unary operator" receives only one argument.
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.