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

  • MHB
  • Thread starter chris2
  • Start date
In summary, the problem on page 62 is caused by two calculation mistakes. The correct formula is: $$-(2) = 2^8 - 2 = 256 - 2 = 254$$
  • #1
chris2
3
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
  • #2
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.
 
  • #3
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?
 
  • #4
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.
 
  • #5
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.
 
  • #6
Thanks for your help. I don't understand it either but am just going to move on:)
 
  • #7
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.
 

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

1. How does two's complement work?

Two's complement is a method of representing negative numbers in binary form. It works by flipping all the bits in a binary number and then adding one to the result. This allows for simple addition and subtraction operations in computers, as negative numbers can be represented using the same logical operations as positive numbers.

2. Why is two's complement used in computers?

Two's complement is used in computers because it is a simple and efficient way to represent negative numbers. It also allows for easy addition and subtraction operations, which are common in computer programs.

3. How do you convert a negative number from two's complement to decimal form?

To convert a negative number from two's complement to decimal form, start by flipping all the bits in the binary number. Then, add one to the result. Finally, convert the resulting binary number to decimal form using the standard binary-to-decimal conversion method.

4. Can two's complement be used for non-integer numbers?

No, two's complement is only used for representing negative numbers in binary form. It cannot be used for non-integer numbers.

5. What is the range of numbers that can be represented using two's complement?

The range of numbers that can be represented using two's complement depends on the number of bits used. For example, with 8 bits, the range is -128 to 127. With 16 bits, the range is -32,768 to 32,767. The maximum and minimum values for a given number of bits can be calculated using the formula -2^(n-1) to 2^(n-1)-1, where n is the number of bits.

Similar threads

Replies
2
Views
2K
Replies
37
Views
3K
Replies
0
Views
1K
Replies
13
Views
5K
Replies
7
Views
2K
Replies
1
Views
2K
Back
Top