Use the shift operators. A shift moves all the bits right or left.
Shift. 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.
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.
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.
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 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.Binary Representation