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

  • Context: MHB 
  • Thread starter Thread starter chris2
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the calculation of the two's complement representation of -2, specifically addressing the confusion surrounding the expression -(2) = 28 - 2 = -126. Participants clarify that the correct calculation should yield 254, not -126, by applying the two's complement method: flipping the bits of 2 (00000010) to get 11111101 and adding 1, resulting in 11111110. The conversation also highlights the importance of understanding the bit representation and the range of values in an 8-bit signed integer, which spans from -128 to 127.

PREREQUISITES
  • Understanding of binary number representation
  • Familiarity with two's complement arithmetic
  • Knowledge of signed and unsigned integers
  • Basic concepts of overflow in binary calculations
NEXT STEPS
  • Study the two's complement method in detail
  • Learn about binary arithmetic and overflow handling
  • Explore the differences between signed and unsigned integers
  • Investigate the implications of bit representation in computer architecture
USEFUL FOR

Computer science students, software developers, and anyone interested in understanding binary arithmetic and two's complement representation in programming and computer architecture.

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.
 

Similar threads

Replies
2
Views
664
Replies
3
Views
4K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
4K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
908
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 0 ·
Replies
0
Views
3K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
7K