How to Use a Two's Complement in a Bit Shift
Two's complement notation is a method of expressing negative numbers using binary bits. Because computer instructions are in binary, two's complement is necessary in programming. It is significantly faster than other forms of arithmetic because no translation is required. The following steps will show how to use a two's complement in a bit shift.
Instructions
-
-
1
Study ordinary binary notation. The rightmost place represents 2^0 or 1 and each successive place to the left represents an additional power of 2. Therefore, the second place to the right is 2^1 or 2, the third place to the right is 2^2 or 4.
-
2
Convert a binary value to a decimal value. We will sum all of the powers of 2 that have a 1 in the corresponding location of the binary value. For example, if the binary value is 1101, then the decimal equivalent is 1x2^3 + 1x2^2 + 0x2^1 + 1x2^0 = 8+4+0+1 = 13.
-
-
3
Examine two's complement notation. The leftmost place is a sign bit with a 1 indicating a negative number and the other bits are magnitude bits.
-
4
Calculate a value in two's complement notation the same as an ordinary binary value if the leftmost digit is 0. If the leftmost digit is a 1, 2^n is subtracted from the ordinary binary value where n is the number of digits in the value. For example, in Step 2, 1101 is 13 in ordinary binary notation. In two's complement notation, however, 1101 = 13 - 2^4 = 13 - 16 = -3.
-
5
Observe the effect of a two's complement value on a bit shift. The sign bit is preserved, so a right bit shift by n places of a two's complement value will divide that value by 2^n and round down. Similarly, a left bit shift of a two's complement value will multiply that value by 2^n provided there is no overflow.
-
6
Consider a bit shift on a specific two's complement value. For a left bit shift, 1101 (-3) becomes 1010 (-6). A right bit shift will make 1101 (-3) become 1110 (-2).
-
1