In two additional representations, the only thing about the powers of two is that they consist of all 0 bits except for the kth bit, where n = 2 ^ k:
base 2 base 10 000001 = 1 000010 = 2 000100 = 4 ...
To get a negative value in two additions, you flip all the bits and add them. For powers of two, this means that you get a bunch of 1 with left and up to 1 bit, which was in a positive value, and then a bunch of 0 with right:
n base 2 ~n ~n+1 (-n) n&-n 1 000001 111110 111111 000001 2 000010 111101 111110 000010 4 000100 111011 111100 000100 8 001000 110111 111000 001000
You can easily see that the result of columns 2 and 4 will be the same as column 2.
If you look at other values โโthat are not in this graph, you can understand why this is not done for anything but two:
n base 2 ~n ~n+1 (-n) n&-n 1 000001 111110 111111 000001 2 000010 111101 111110 000010 3 000011 111100 111101 000001 4 000100 111011 111100 000100 5 000101 111010 111011 000001 6 000110 111001 111010 000010 7 000111 111000 111001 000001 8 001000 110111 111000 001000
n & -n will (for n> 0) only 1 bit be set ever, and this bit will be the least significant set bit in n. For all numbers that are powers of two, the least significant bit of the set is the only bit set. For all other numbers, there is more than one bit set, from which only the smallest value will be specified as a result.
Eclipse Sep 13 '11 at 20:09 2011-09-13 20:09
source share