MHB Two's Complement Help: Solve -(2) = -126

  • Thread starter Thread starter chris2
  • Start date Start date
AI Thread Summary
The discussion revolves around the confusion regarding the calculation of -(2) in two's complement representation, specifically why it equals -126 in the example from "Computer Science Illuminated." Participants clarify that the correct calculation should yield 254, not -126, as the two's complement of -2 in an 8-bit system is represented as 11111110. There is also a debate about whether the example is using 7 bits for the number, with the 8th bit as a sign bit, which leads to misunderstandings regarding the representation of negative numbers. Ultimately, the consensus is that the example's calculation is incorrect, and the proper method involves flipping the bits and adding one to achieve the correct two's complement representation. The thread emphasizes the importance of understanding the range and representation of integers in binary systems.
chris2
Messages
3
Reaction score
0
Hi,

I'm reading Computer Science Illuminated and on page 62 it discusses two's complement. A problem they show (on the same page) is:

-(2) = 28 - 2 = 128 - 2 = -126

I have stared at this and reread what is before and after it. I just don't understand why it's negative 126. It's not a misprint because the next page continues to use it for other ways to use two's complement.

Thanks for any advice.
 
Technology news on Phys.org
chris said:
Hi,

I'm reading Computer Science Illuminated and on page 62 it discusses two's complement. A problem they show (on the same page) is:

-(2) = 28 - 2 = 128 - 2 = -126

I have stared at this and reread what is before and after it. I just don't understand why it's negative 126. It's not a misprint because the next page continues to use it for other ways to use two's complement.

Thanks for any advice.

Hi chris! Welcome to MHB! :)

It must be a misprint, because it contains two calculation mistakes!
It should be:
$$-(2) = 2^8 - 2 = 256 - 2 = 254$$

The two's complement value of -2 is really 254 and definitely not -126.
Note that if you add 2 to 254, you get 256.
Since this won't fit into an 8-bit register, you're left with 0 as expected.
 
You may want to provide more of the problem from the book. The one liner shown does not really illustrate what it is trying to do.

Whenever you do a 2's complement, you (a) perform a ! (or NOT) on the binary, and (b) add 1. So the number $2$ in 8-bit is $00000010$.

To get $-2$, first you flip the bits $11111101$, and then add $1$, it becomes $11111110$. Note the for an 8-bit signed integer, this takes on the range $[-128,127]$, and the number $-128$ is designated the binary $10000000$. The 2's complement of $-128$ is $-128$ itself as there is no $128$ in the range.

Still not sure where the $-126$ comes from - is that you want to show $(-2) + (-126) = (-128)$ to get at the absolute minimum?
 
Thanks for the replies. One of the authors wrote me back and said the formula is correct because the example is using 7 bits for the number with the 8th bit being the sign, so you take 2 to the 7th power. The result is a number in decimal, which is then converted to octal and then binary.

On the next page the problem is done exactly how you said where you flip the bits and add 1, which I understand, but I still have trouble understanding the way the formula above is written or if I saw a similar problem that I'd know what I was looking at.
 
chris said:
Thanks for the replies. One of the authors wrote me back and said the formula is correct because the example is using 7 bits for the number with the 8th bit being the sign, so you take 2 to the 7th power. The result is a number in decimal, which is then converted to octal and then binary.

I do not know what this author is writing about.
It appears there is some miss communication.

Either way, my point remains the same.
In two's complement with 8 bits, we do not take 2 to the 7th power - we take 2 to the 8th power.
The 8th bit is indeed a sign bit though.

Nothing in the line you gave is octal nor binary.
So if anything is, it comes later.
 
Thanks for your help. I don't understand it either but am just going to move on:)
 
As I understand it, in an $N$-bit register, one represents $-k$ as:

$2^N - k$ in binary. This allows an integer range of:

$[-2^{N-1},2^{N-1} - 1]$.

In this case, $N = 8$, so we can only represent integers in the range -128 to 127.

So in this representation, we have:

-2, represented as the binary number (100000000 - 10) which equals: 11111110.

(Note that even though the number we are SUBTRACTING from has 9 digits, any lesser number will only need 8).

The first digit (1) tells us the sign is negative, the next 7: 1111110, when converted to base 10 give:

$2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2 = 64 + 32 + 16 + 8 + 4 + 2 = 126$,

and indeed: $128 - 126 = 2$, and with the first bit for the sign, we get -2.

I think some of the confusion comes from the fact that we have TWO meanings for the binary strings:

a) an unsigned value
b) a signed value (two's complement value).

Note that the procedure that magneto outlines is perfectly correct: the reason why it works is because we "ignore the overflow bits" (in other words, we are actually working mod $2^N$).

Thus two's complement integer data implementations perfectly mimic the arithmetic structure of the integers, AS LONG AS we do not exceed the storage capacity. For example, if you are working on a computer system that has 64-bit architecture, it would be unwise to have more than 19 quintillion files, or require precise computations on integer values up to 30! (factorial). Calculators, and calculation programs get around this by splitting numbers up into smaller numbers in various clever ways.
 
Back
Top