Help me understand 2s-complement binary representation of negative values
binary - Why Two's Complement works - Mathematics Stack Exchange
binary - What is “two's complement”? - Stack Overflow
Two Complement Binary. Totally lost.
What is the two's complement?
The two's complement is a way to represent negative numbers in binary when the minus sign is not available. The minus sign is substituted in the two's complement representation by a digit, usually the leading one.
- If the leading digit is
0, the number is positive. - If the leading digit is
1, the number is negative.
What is the 8-bit two's complement notation of -37?
The 8-bit two's complement representation of −37 is 110110112. To find this result:
- Subtract 37 from 27: 128 − 37 =91.
- Find the binary representation of 91:
91 = 64 + 16 + 8 + 2 + 1
= 1·26 + 0·25 + 1·24 + 1·23 + 0·22 + 1·21 + 1·20
=1011011 - Place 1 in the correct position to mark that we started from a negative number:
−3710 = 110110112
How do I calculate the two's complement of a number?
To calculate the two's complement of a number:
- If the number is negative, subtract it from the power of 2 with exponent corresponding to the number of bits of your chosen representation.
- Convert the number to binary.
- If the number was negative, add
1to the proper position and pad with0. - If the number was positive, left-pad the result with
0to the desired length.
Videos
I am following a tutorial that states to get the binary reprsentation of a negative value, you invert the bits.
So 20 in binary is
0001 0100
Then -20 is
1110 1011
But dont understand why this isnt 235, and how do you represent 235, if the above is -20?
I'll stick to 8-bit quantities, but the same applies in general.
The key to understanding two's complement is to note that we have a set of finitely many (in particular, $2^8$) values in which there is a sensible notion of addition by $1$ that allows us to cycle through all of the numbers. In particular, we have a system of modular arithmetic, in this case modulo $2^8 = 256$.
Intuitively, arithmetic modulo $n$ is a system of addition (and subtraction) in which overflow and underflow cause you to "cycle back" to a value from $0$ to $n-1$. A classic example is the usual "clock arithmetic", which is to say arithmetic modulo $12$.
For example, if it is $11\!:\!00$ now, then three hours later it will be $2\!:\!00$, since $$ 11 + 3 = 14 \equiv 2 \pmod {12} $$ and similarly, if it is $1\!:\!00$, then $4$ hours ago it was $9$ since $$ 1 - 4 = -3 \equiv 9 \pmod{12} $$ Notice that subtracting $4$ hours on the clock is the same as adding $12 - 4 = 8$ hours. In particular, we could have computed the above as follows: $$ 1 - 4 \equiv 1 + 8 = 9 \pmod{12} $$ That is: when performing arithmetic modulo $n$, we can subtract $x$ by adding $n-x$.
Now, let's apply this idea modulo $2^8 = 256$. How do you subtract $3$? By the above logic, this is the same as adding $256 - 3 = 253$. In binary notation, we could say that subtracting $00000011$ is the same as adding \begin{align} 1\overbrace{00000000}^8 - 00000011 &= (1 + \overbrace{11111111}^8) - 00000011 \\ & = 1 + (11111111 - 00000011) \\ & = 1 + 11111100 \\ & = 11111101 \end{align} and there's your two's complement: the calculation $(11111111 - 00000011)$ "flips the bits" of $00000011$, and we add $1$ to this result.
Note 1: In the context of arithmetic with signed integers, we don't think of $11111101$ as being $253$ in our $8$-bit system, we instead consider it to represent the number $-3$. Rather than having our numbers go from $0$ to $255$ around a clock, we have them go from $-128$ to $127$, where $-x$ occupies the same spot that $n - x$ would occupy for values of $x$ from $1$ to $128$.
Succinctly, this amounts to saying that a number with 8 binary digits is deemed negative if and only if its leading digit (its "most significant" digit) is a $1$. For this reason, the leading digit is referred to as the "sign bit" in this context.
Note 2: An interesting infinite analog to the two's complement system of subtraction is that of infinite series 2-adic numbers. In particular, we can say something strange like $$ \dots 11111 = -1 $$ since $\dots 11111$ is the "infinite two's complement" of $1$.
Let's look at a decimal example. You want to do $735-78$.
Borrow 1000 from the Number Bank; the loan is subject to no interest, but you must give back what you got as soon as you have used it.
Now consider that $$ 735-78=735+(1000-78)-1000 $$ The subtraction $1000-78$ is very easy to do: just do $9$-complement on the rightmost three digits (the missing one at the far left is, of course, $0$), getting $921+1$, so our operation now reads $$ 735-78=735+921+1-1000 $$ Since \begin{array}{rr} 735 & + \\ 921 & = \\ \hline 1656 \end{array} we can give back 1000 to the bank and add 1: $$ 735-78=656+1=657 $$
In base two it's exactly the same, with the only difference that $1$-complement (instead of $9$-complement) is very easy, because it consists in flipping the digits. You don't need the loan either, because you work on a fixed number of bits, and numbers that overflow are simply reduced forgetting the leftmost digit. So if you have to do
00101001 - 00001110
you can flip the digits in the second number and add, forgetting the leftmost bit that may become 1:
00101001 +
11110001 =
----------
00011010 +
1 =
----------
00011011
Two's complement is a clever way of storing integers so that common math problems are very simple to implement.
To understand, you have to think of the numbers in binary.
It basically says,
- for zero, use all 0's.
- for positive integers, start counting up, with a maximum of 2(number of bits - 1)-1.
- for negative integers, do exactly the same thing, but switch the role of 0's and 1's and count down (so instead of starting with 0000, start with 1111 - that's the "complement" part).
Let's try it with a mini-byte of 4 bits (we'll call it a nibble - 1/2 a byte).
0000- zero0001- one0010- two0011- three0100to0111- four to seven
That's as far as we can go in positives. 23-1 = 7.
For negatives:
1111- negative one1110- negative two1101- negative three1100to1000- negative four to negative eight
Note that you get one extra value for negatives (1000 = -8) that you don't for positives. This is because 0000 is used for zero. This can be considered as Number Line of computers.
Distinguishing between positive and negative numbers
Doing this, the first bit gets the role of the "sign" bit, as it can be used to distinguish between nonnegative and negative decimal values. If the most significant bit is 1, then the binary can be said to be negative, where as if the most significant bit (the leftmost) is 0, you can say the decimal value is nonnegative.
"Sign-magnitude" negative numbers just have the sign bit flipped of their positive counterparts, but this approach has to deal with interpreting 1000 (one 1 followed by all 0s) as "negative zero" which is confusing.
"Ones' complement" negative numbers are just the bit-complement of their positive counterparts, which also leads to a confusing "negative zero" with 1111 (all ones).
You will likely not have to deal with Ones' Complement or Sign-Magnitude integer representations unless you are working very close to the hardware.
I wonder if it could be explained any better than the Wikipedia article.
The basic problem that you are trying to solve with two's complement representation is the problem of storing negative integers.
First, consider an unsigned integer stored in 4 bits. You can have the following
0000 = 0
0001 = 1
0010 = 2
...
1111 = 15
These are unsigned because there is no indication of whether they are negative or positive.
Sign Magnitude and Excess Notation
To store negative numbers you can try a number of things. First, you can use sign magnitude notation which assigns the first bit as a sign bit to represent +/- and the remaining bits to represent the magnitude. So using 4 bits again and assuming that 1 means - and 0 means + then you have
0000 = +0
0001 = +1
0010 = +2
...
1000 = -0
1001 = -1
1111 = -7
So, you see the problem there? We have positive and negative 0. The bigger problem is adding and subtracting binary numbers. The circuits to add and subtract using sign magnitude will be very complex.
What is
0010
1001 +
----
?
Another system is excess notation. You can store negative numbers, you get rid of the two zeros problem but addition and subtraction remains difficult.
So along comes two's complement. Now you can store positive and negative integers and perform arithmetic with relative ease. There are a number of methods to convert a number into two's complement. Here's one.
Convert Decimal to Two's Complement
Convert the number to binary (ignore the sign for now) e.g. 5 is 0101 and -5 is 0101
If the number is a positive number then you are done. e.g. 5 is 0101 in binary using two's complement notation.
If the number is negative then
3.1 find the complement (invert 0's and 1's) e.g. -5 is 0101 so finding the complement is 1010
3.2 Add 1 to the complement 1010 + 1 = 1011. Therefore, -5 in two's complement is 1011.
So, what if you wanted to do 2 + (-3) in binary? 2 + (-3) is -1. What would you have to do if you were using sign magnitude to add these numbers? 0010 + 1101 = ?
Using two's complement consider how easy it would be.
2 = 0010
-3 = 1101 +
-------------
-1 = 1111
Converting Two's Complement to Decimal
Converting 1111 to decimal:
The number starts with 1, so it's negative, so we find the complement of 1111, which is 0000.
Add 1 to 0000, and we obtain 0001.
Convert 0001 to decimal, which is 1.
Apply the sign = -1.
Tada!
Most of the explanations online just explain the instructions on how to get the two complement in binary("invert everything +1" or "start from the lowest bit that's 1 and every bit after that invert it") but I haven't been able to find the reasoning behind it. When I say this I don't mean "why is it used as opposed to 'sign magnitude' " (that I understand with the additions being correct and everything being neat)
How did the originator come up with this way of thinking. The more I think about WHY and HOW it's done this way the more confused I get(Just goes to show the guy who discovered it is a genius).How would you do the same in our base 10/decimal system? As in what would the complements be in our decimal system.
I watched this video
https://youtu.be/JTFp0rRF30o?si=8kl5SuRc2zF0PJ30
but unfortunately I'm still a bit confused behind the proof for two complement system.
Thank you very much.
P.S: It doesn't help that I think of "2 Compliments" every time I read the name. On a side note, what is being completed here?