Reputation: 59
i have make an exercice but i'm not sure thanks to help me :
11001011 on 8 binary digits.
Questions :
i reverse : 00110100 , + 1 : 00110101
Maybe still 00110101
Upvotes: 0
Views: 1966
Reputation: 18940
In two's-complement representation, positive numbers are simply represented as themselves, and negative numbers are represented by the two's complement of their absolute value.
In two's-complement representation, a number is negative if (and only if) the highest bit is set.
So, if you want to get the absolute value, you reverse the two-complement operation only if the highest bit is set.
For example, using 8 bits, the number 5 is represented as
00000101
The highest bit is 0, so it represents a positive number. To convert it to -5 in two's-complement notation, invert it and add 1:
~00000101 + 1 = 11111010 + 1 = 11111011
To take its absolute value, check if the highest bit is set (it is), and undo the two's-complement operation, that is, subtract 1 and invert bits:
~(11111011 - 1) = ~11111010 = 00000101 = 5
If the highest bit is 0, then it is a positive number, and it already is its absolute value.
Upvotes: 1