Previously felt the shift operator is quite clear, perhaps the study of time long, and then a look, forget almost. Now refer to some of the online learning materials, the displacement operator collation, as a summary of knowledge points, but also a cumulative. Before you speak the shift operator, simply add the knowledge of the original code and the complement.
I. Original code and complement
In a computer system, values are stored (expressed) in complement. The main reason: with the complement, the sign bit and other bits can be processed uniformly, and subtraction can also be processed by addition. In addition, when the number of two complement representations is added, the carry is discarded if the highest bit (sign bit) has rounding.
1. The original code is divided into two types of complement
(1) Positive complement: the same as the original code (the original code is known to complement)
For example: +9 of the original code is 0000 1001. The complement is also 0000 1001.
(2) The complement of the negative number: The sign bit is 1, the remaining bits for the absolute value of the original code bitwise reversed, and then the entire number plus 1.
For example: 7 The original code is 1000 0111 (the high position is the sign bit, 1 is negative, 0 is positive), and the complement is 1111 1001.
2. Complement to the original code is also divided into two cases (known as the complement to seek the original code)
(1) The original code of the positive number: same as the complement
For example: +9 of the complement is 0000 1001. The original code is also 0000 1001.
(2) A negative number of the original code: The sign bit is 1, the rest of the complement is reversed by bitwise, and then the whole numbers plus 1.
For example:-7 of the complement is 1111 1001, the original code is 1000 0111.
Two. Shift Operators
The "<<" and ">>" operators are used to perform shift operations, called the left and right shift operators, respectively. For X<<n and x>>n operations, the meaning is to move x left or right n bits, resulting in the same type as x. Here, the type of x can only be int,uint, long, or ulong,n type can only be int,n type is only int, or the display is converted to one of these types, or the mutation will be error.
1. Left shift operator
Use the left shift (<<) operator to shift the number to the left. The effect is that all the bits are shifted to the left for a specified number of times, the higher bits are lost, and the lows are filled with zeros.
Note: If the first operand is an int or uint (32-digit number), the shift number has a lower 5 bits of the second operand given (the reason is that the range of low 5-bit can be represented is 0~24+23+22+21+20, which is 0~31, which is enough for a 32-bit shift to use), If the first operand is a long or ulong (64-digit number), the number of shifts is given by the lower 6 bits of the second operand (for the same reason). when left-shifted, the high-order bit of the first operand is discarded, and the lower-order vacancy is filled with 0. A shift operation never causes an overflow.
(1) positive left shift (with 85 as an example, can be considered as an int, long, uint, ulong, as a type of uint, 32 bits)
85 binary representation: 0000 0000 0000 0000 0000 0000 0101 0101
85 Shift Left (<<) 3-bit: 0000 0000 0000 0000 0000 0010 1010 1000
Post-shift results in decimal notation: 680
(2) negative left shift (with-85 as an example, can be regarded as an int, one of the long, here as int,32 bit)
-85 of the binary complement means: 1111 1111 1111 1111 1111 1111 1010 1011
-85 shift Left (<<) 3-bit: 1111 1111 1111 1111 1111 1101 0101 1000
The original code of the result after displacement: 1000 0000 0000 0000 0000 0010 1010 1000
Post-shift results in decimal notation: 680
2. Right shift operator
The right shift operator (>>) shifts the number to the right, and the effect is that all the bits move to the right for the specified number of times.
Note: If the first operand is an int or uint (32-digit number), the shift number has a lower 5 bits of the second operand given (the reason is that the range of low 5-bit can be represented is 0~24+23+22+21+20, which is 0~31, which is enough for a 32-bit shift to use), If the first operand is a long or ulong (64-digit number), the number of shifts is given by the lower 6 bits of the second operand (for the same reason). if the first operand is an int or long, the right shift is the arithmetic shift (the high-order vacancy is set to the sign bit). If the first operand is a uint or ulong type, the right shift is a logical shift (high fill 0).
(1) Positive right shift (take 85 as an example, can be considered as an int, long, uint, one of the ulong, according to which rules are OK)
85 binary representation: 0000 0000 0000 0000 0000 0000 0101 0101
85 Right Shift (>>) 3-bit: 0000 0000 0000 0000 0000 0000 0000 1010
Post-shift results in decimal notation: 10
(2) Negative right shift (with-85 as an example, can be regarded as an int, one of the long, here as int,32 bit)
-85 of the binary complement means: 1111 1111 1111 1111 1111 1111 1010 1011
-85 shift Left (>>) 3-bit: 1111 1111 1111 1111 1111 1111 1111 0101
The original code of the result after displacement: 1000 0000 0000 0000 0000 0010 0000 1011
Post-shift results in decimal notation: 11
C # Shift operators ("<<" and ">>") detailed description