Twos complement: Negative numbers in binary
HTML-код
- Опубликовано: 30 сен 2024
- How can we represent negative numbers in binary? There are several ways. This video compares using a sign bit, ones complement, and twos complement. Twos complement is the most commonly technique because it's relatively easy to implement in hardware and it makes addition and subtraction with negative numbers easy.
Support me on Patreon: / beneater
Please tell me you're a paid instructor, and that you're paid well. You deserve it! This isn't just a RUclips series, this is a full fledged course Thank you!!
He used to be employed by Khan Academy. I'm not sure, maybe still is.
@@IvanEedle monetized youtube? He has 400K subscribers.
ruclips.net/video/pJ-zzHEsXZE/видео.html
2's complement shortcut
@@dendritedigital2430 RUclips's monetization is a joke; they don't pay creators anywhere near industry standard. Half a *million* views is about $1,000 based on my experience making videos (and according to Social Blade he got $246 at worst, $2,000 at best). To get the avg salary of a Computer Science Professor he would need to do that 114 times a year (10 videos like this every month, excluding how many videos might be flops, excluding weekends, etc.) But that's an impossible expectation. This is his 9th most viewed video of all time. Social Blade estimates his revenue at $296 - $4.7k monthly (probably $2,500). That's minimum wage. I hope I'm wrong and he's getting more than that. I wonder if he has a Patreon?
@@ReasonMakes Patreon, Development Kits, etc...
I've tried to understand two's compliment before with no luck. This is the first time its made sense. Thanks!
This video and the assembly to c code video might take the crown for most saved computer architecture exams😊
This is actually a pretty comparable explanation of 2's comp to I got in my college digital design class. The one bonus I did get from that class though that you didn't mention is a really easy way to get to 2's comp. Bring down each bit until you reach your first 1, bring that 1 down, and then invert all of the other bits. Quick way of doing it that negates the need for that two step process.
Thank you Crimson, it does make the process quicker indeed
Such a great explanation I was able to grasp the concept from the first explanation! Teaching a man how to fish.
Absolute hero. As another commenter said, we did this loads at uni but it was never explained in terms of the MSB being negative. Now everything has instantly clicked. You are a truly world class educator.
Greatly explained! Thank you very much
While a great video, I would like to call out one more point which makes the need of 2's compliment even more intuituve.
- If you would look at 1's compliment, the discrepancy between binary representations and decimal representation is that there is no -0 in decimal because of which negative binary numbers are all erroring by one place in the rank.
- A good exercise is trying multiple additions and subtractions in 1's compliment form - You will notice +ve +ve works correctly. +ve -ve works correctly if result is -ve (because both operator and result get shifted by 1), Even -ve -ve doesn't work correctly because of the shifted number system in -ve zone which starts with -0.
- 2's compliment does nothing but removes -0 from the timeline.
I like the way he says "bit"
thanks for this video
Amazing explaination! So where is the "next video"?🥲
I probably had two's compliment taught to me half a dozen times in college. I get how it works. The properties are nice. Not once has anyone ever told me "treat the sign bit as a -(2^n) bit." This is a borderline life-changing revelation.
@美遊エーデルフェルト why?
Some people need to visualize things, which in turn gives deeper understanding of subjects. I found this video while i was searching for a better understanding of 2s compiment and 1000 == -8 was the piece i needed to feel i understand it, because it helped visualize the whole thing. And in turn visualize very basic ALU operations better.
SAME HERE
@@crs-t how do we write positive 8 then 😅😅😅
@@alonebuthappy33 You use more bits :)
If you have 4 bits and 1 is for sign, you actually have only 3 bits for numbers, so you can represent at most 0b111 = 7.
By far the best explanation of two's complement.
especially the -8 part
@@oussamaelhriki8160 true
true
For sure!!
It really is. I'm reading Harris' "Digital Design and Computer Architecture" which covers binary numbers in chapter 1 and although it's a fantastic book, its explanation still falls short of Ben's. Ben really shows the logic behind the pattern for those of us who need it spelled out. Mr. Eater you the man.
Where were you when I was in college? Awesome explanation
That moment when you find a great channel, but the last upload is 2 months ago. And then the next day 3 videos go up.
wat
did university teachers leave so many dislikes because they are jealous how this guy easily explains hard concepts for free ?
Oh my gosh the two's compliment is beautiful and I realized this at 12:18 that -8 makes so much sense
Especially because you have 4 bits so you can have 16 numbers. Given that 0 has no sign, you have to assign 1 more number to either positive or negative side.
It's not only -8 ;) when you have more bits the most significant bit is always treated as a negative value of the highest bit order. If you really compare the signed representation to 2s complement you'll find that the series just goes in reverse ;). And the extra negative number is there only because there is 1 zero
i didnt understand it until this timestamp thanks lol
Been fiddling with bits for 20 years and I don't think i've ever seen this explained as clearly as this. The video is a joy to watch, great work!
12:06 Wow! This is the first time I'm seeing someone assigning a (negative) place value to the sign bit, and it makes sense indeed! :o :D And I thought that I won't learn anything new from a two's complement arithmetics video. Silly me... :D
This blew my mind. I wish I was taught this earlier
very wonderful
@@MozartJunior22 for real
Wow, best explanation ever. You explained it! In school they just say, here is the Two's complement, take it and don't ask why it works.
This is far better explanation than the paid course at my university. Thank you so much.
the way you first worked with flawed methods and then built your way to show us why adding 1 to the one's complement works better was really amazing . thank you so much
This was buffling me for sometime.Thank you.
Another neat property of two's complement is sign extension.
In some architectures you'll often find yourself converting a number between sizes. So for example, we have a 4-bit number we want to resize to an 8-bit number.
1010 = -6
When we extend the sign, what this means is when we convert to a larger number, every added bit copies the value of the most significant bit from the smaller number.
11111010 = -6
This also works on positive numbers:
0100 = 4
00000100 = 4
The only drawback is ou have to make sure your hardware does not keep trying to sign-extend on an unsigned number.
1000 = 8 (Unsigned)
11111000 = 248 (Unsigned)
Fortunately, the same hardware used to drive signed number behavior in twos-complement should be able to also ensure sign extension only kicks in when it's in use.
O:
my mind has been blown for the second time today
A great way to explain not just the process of negative binary numbers but actually why two's complement is used.
Exactly!
My random opinion as a random person would be that: considering the fact that 1 is being subtracted from each bits. We end up getting it's negative version. But... Since the représentation is basically 1 less so after everything we have to add back this 1 that we have taken from. But that is not full fledged and doesn't explain the fact that we have taken 1 from each bit. But collectively add 1 and we get to the original result. It's odd fr. 🤷♂️
Nice, you just said something that made sense.
When I was in computer class in school, we were presented with a lesson on binary and it wasn't explained why these are one's or two's complement, just that is what they were called.
You are a hero in my book. Amazing how you present these video's, you keep it interesting and the build-up is just right. Keeping it interesting while teaching is important but I always think that making people feel smart while teaching is equally important. In my opinion you nail both.
Thomas Cremers couldn’t have put it better
A couple of years ago I had to learn those things for university, and I did, and got a good score, but due to the fact that I'm going to be a SW Developer that programs microcontrollers, I had to re-learn these things and watching this I've realised that I didn't understand the complement method the way I was supposed to. My teacher just told us to add 1 and that's it and now, after 4 years, I have finally understood why. You are awesome. I really hope a lot more students watch your videos.
I never understood the need for 2's compliment before this video
Thank you so much
I have sat through a few bad explanations of what's going on here... probably due to the tutor/lecturer not understanding it themselves.
Video time: 12:15
To get the value (decimal numeral) of "looks like a sign bit" immediately, do this calculation: -(2^(n-1))
Where n is the number of bits you have. For example: If you have 16 bits, the calculation is: -(2^(16-1)) = -32768
Video time: 12:48
An easier way to get a negative value from a positive one, is to flip all the numbers starting from "looks like a sign bit" until you get to the last 1, leave it as a 1 (don't flip). The rest of the 0's after the last 1, leave it as a 0 (don't flip).
3 examples all in eight bits:
A) 10 is 00001010 , so to get -10 in eight bits is 11110110
B) 20 is 00010100 , so to get -20 in eight bits is 11101100
C) 56 is 00111000 , so to get -56 in eight bits is 11001000
This has helped me to implement a binary indexed tree efficiently. Thanks!
to the 100 people who disliked: That's not the download button
I remember at the digital logic lab when we had to design a 4-bit ALU that included logic AND, OR, XOR and add and sub, and with a 2-bit select signal we had to choose any one of the operations. As bonus, we had to build any other adder, except from ripple carry, display the result on a seven segment display in two digits and find a way to display the minus sign for negative numbers by using the third seven segment display and the negative numbers as positive ones next to the minus sign. I was the one who managed to do all these three, and I chose to implement a carry look ahead adder
"5 minus 3 is 1. That's close!" =)
Glad to see this series continuing
I see a lot of people with blown minds in the comments, at the fact that the "sign bit" is actually just a negative-valued bit. I'm *so glad* I was taught it like this in university. It just fit right into my head. It made sense from day one.
Ones and twos complement:
Ones complement, invert all the values.
Twos complement, invert all values after the FIRST 1.
e.g.:
010110111000
Ones complement:
101001000111
Twos complement:
101001001000
Wow,Amazing this helped me a lot.Thanks
Here's a simple on
One's Complement : Invert all values
2's Complement : Add 1 to the one's complement
Fantastic video, really well done. I though it would be cool to additionally mention is the weird behavior at the boundaries e.g. when you take 0111 and add 1... you get 1000 (-8!) also the even more bizarre case where you try to negate 1000 (-8)... pretty sure you get 1000 (-8 again!) this last one actually comes up in one of the exercises (3-4) in the canonical tome 'The C Programming Language'
Yea, I'm pretty confused too, like with say 4 + 4 = -8, or -7 + -7 = 2 ... I was hoping this video would answer how to handle this but ah well
@Abbas Mousavi Yes, I see you understand what I mean. My problem is that how would I get a machine to understand that when you have 7 + 7 = -2, that it is actually a wrap around *and* a negative two? Giving 14, as this system is modulo 16?
Wish I had found your videos earlier in the semester. You've been better in a few minutes than my professor all semester. Click comment, but very true.
13:06 My professor always had us yell, "Flip those bits!" when talking about twos complement. :D Great video!
The one’s complement could also get the correct answer by simply adding the extra bit when adding the two numbers. For example, 5 + (-3) will result into 10001. Since it should only have four bits, there is an ‘1’ excess bit which is called the end-around carry (EAC). You can simply add the excess bit (0001 + 1) to get 0010 which is 2 in its decimal equivalent.
first time i undrstood this even after using them many times
Thank you so much for this explanation, I've been struggling with this on the coursera "nand to tetris" course, this is by far the best explanation I can find. cheers.
I never realized you can consider the sign bit as the negative of the power. "-8's place" Whao!
Other than the purpose of doing addition, what are the scenarios where you actually do use ones complement say to do some other kind of computation?
I often find it dificult to locate the 'next' video. The one mentioned in this item for example. Could you consider updating the comments with the next vid or using a vid number sequence perhaps Ben? Thanks in advance.
Hats Off to you .. i wish i could pay you all of my engineering fees!
I took a couple semesters of computer hardware design before deciding I hated the school.
Whenever I did two's-complement binary subtraction, I skipped the second step of adding a 1 after the invert and add, and just carried a 1 into the add step.
Given that's what your ALU is going to do, I feel like learning to do it on paper in the single step cane be useful.
(though, nevertheless, the "professor" was always confused when I added like that, but then math teachers have always been against combining steps for some reason too)
Thank youuuuuu, this has been confusing me about *why* we do it like this, and your -8 (or negative MSB) trick really makes it go from "a nuisance but necessary" to "check out this cool trick".
Congratulations for a new subscriber . you explain so well 😊😊👍👍 thanks
just treat the -1 as the highest value 3 bits can represent , -2 as (the highest value 3 bits can represent) + 1 , and so on. Just keep adding 1 number each time . easy to remember
I feel really smart now considering how my own professor refered our class to this video and in this video, you came to the same conclusion as I did when I realized that the negative numbers are just -8 + whichever positive number the remaining 3 bits represent.
THIS IS THE BEST EXPLANATION EVER ! I LITERALLY LOGGED ON TO COMMENT ON THIS VIDEO.
KEEP UP THE MIND BLOWING WORK
1's compliment+1= 2's compliment and 1's compliment is just invert to the given binary no
08:19 you made a mistake. 1's compliment addition isn't always off by one. It is only off by one when the overflow bit is 1 (if we assume -0 is correct). when the overflow bit is 1, adding 1 gives the correct answer.
Love it! Please do this for float (double).
That -8 bit place is what did it for me, thank you. (learning 68K assembly currently and the tutorial on negative numbers sucks)
Thanks for this - we covered it in class last week and I'd used it before many years ago, but by Friday my brain is a marshmallow.
Fantastic Tutor - Hats Off To You!
most just teach that 2 step process for getting negetive numbers
but u explained how that 2s complement steps works
thats really genius
you are amazing brother
Wow, I never realized that the msb was actually a negative number itself. I always negated to find its value
My university teacher never explained me like this. Thank you very much sir. This is the best explanation on 2's compliment.
I wanted to teach students how to solve pseudo-code and got stuck in bitwise operators. By far the clearest explanation of two's complement. Thank you, sir. I'll be teaching it exactly as you have done here.
What happens if you do like 4 + 4 though? You'd get -8...
Why you ain't my college professor that's cap 😭😭😭😭🥺🥺🥺🥺
heeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeehhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh how tf. make sense plz just say if the left most digit is a 1 it turns that digidt in to a negative. if not it doesnt count as anything. 1001 in two's compliment is -7 . soooooooooo mhhhhhmmmmmmmmmmm
I'll leave my own exploration about the overflow problem in case anyone wants to see it as an addendum to this very clear video:
The fundamental math behind two's complement: we choose a modulus to do modular arithmetic (in this case 16), and thus we can add or subtract multiples of the modulus without changing anything. The modulus in this mathematical group we are using is like the zero of the integers. We are in the group Z16. In this case the modulus is 16; in binary, it is expressed 10000 (I'll use binary from now on). What are the multiples of 10000? 100000, 110000, 0000, -10000, -100000, or any binary integer ending in four 0's. That's convenient, because when adding two of our elements, we can just ignore any digit in the 10000's place, the 100000's, place, and so on, and not build any hardware to find those digits. We just need four-bit hardware (or do we?).
I'll go back to base ten now. Mathematicians working with a modulus of 16 usually pick the set of representatives {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, but our case, we choose the representatives {-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7}. Either choice is acceptable, since 15=-1, 14=-2, 13=-3, 12=-4, 11=-5, 10=-6, 9=-7, and 8=-8 mod 16. A problem should now be apparent though: what if we add 1 and 7? Well, in the integers, the answer is 8, but we are in the group Z16, where 8=-8, and our representative for ...=-24=8=-8=24=40=... is -8, not 8. We actually do need additional hardware to deal with this overflow problem, because we are interested in integers, not elements of Z16. There are too many cases to do individual checks for: 7+1,7+2,7+3, ..., 6+2, 6+3, ..., 4+4, ..., and so on. I suppose modular arithmetic can save us from modular arithmetic though. If we instead choose a modulus of 32, and limit ourselves to {-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7} as the set of valid inputs, then the largest number we can get (after lifting back to the integers) is 14, and the least number is -16, both of which would be in the set Z32 = {-16,-15,...,-1,0,1,...,14,15}. Let's write out all 32 of our representatives in Z32:
10000=-16
10001=-15
10010=-14
...
10111=-9
11000=-8
11001=-7
...
11110=-2
11111=-1
00000=0
00001=1
00010=2
...
00111=7
01000=8
01001=9
...
01110=14
01111=15
If we instead think of a representative as just a binary unsigned number written out, then clearly, any number x such that 10000
I'd never thought of the high bit in two's complement as meaning -(2^(n-1)) before. That's incredibly useful. Thank you so much!
the realisation that 8's coulumn is actually -8's coulumn has saved me here. Thanks so much.
i love how you explain everything bit by bit
I see what you did there
Thank you so much! Great video.
Bullshit ends at 9:40 for anyone who value their time.
The best explanation i have ever seen so far on 2's complement.
9:57 So thats why the max value is 255 on many of 8 bit systems
2's compliment sounds like a pokemon trainers card
Your explanation is getting me through my circuits class, well done sir!
Hey! great content. Could I have the link to the "next" video where you talk about subtracting and adding?
Not sure which one is the one since you have so many videos. :) Hope you are staying healthy and happy during this difficult time
Glad I found this video! Thank you!
when he adds 1 at the very end of the video, why is it 1011????
THANK YOU. I have been taught this by 2 instructors at a college. NONE of them simplified this like you did. IT FINALLY CLICKED!!!
Very well explained. Thank you!
I realized for the last part where you convert positive to negative numbers in 2's compliment, it's just the same as remembering the 1's compliment situation...how we had to move one row down to get the right answer.
The moment you relies when you realize: my teacher was $h!t.
Excellent video and just for everyone's' knowledge, in One's Compliment arithmetic, you take your carry-over bit and add it back in on the right side.
I've watched several videos about twos complement and am in a CSCI 101 class where our professor explained it several times. This is the explanation that really landed for me. 10/10 would learn again
thank you so much from the bottom of my heart! i really hope you're having a good life cuz you deserve it.
I don't know how to say thank you. You made this really simple. Some books and videos were driving me nuts ! God bless you man !
You sir earned my sub. ❤️❤️❤️❤️❤️
Really good video and explanation 👍
You are awesome ! I wish I could support you through patreon ! I love your videos !
You goddamn legend. I can't believe I understand this so well.
Why the 8th location indicate the negative number ? also we are using only first 3 locations as our number,so the max is 111 = 7 , what if i want to use more than 7 like , 1011 = 11 , i can't do this ? if i do this so i will not be having any more negative numbers ? so this is means max number to use is 7 ?
for 8, you will go for the extra 4 bits.. so -8 will be 1000 1000 and -255 will be 1000 1111 1111
4 bits unsigned (16 numbers) range ( 0 > 15 )
4 bits signed (16 numbers) range ( -8 > 0 > 7 )
I love this series and i was just wondering if you're going to finish the series by the end of the summer. I'm dying to build one of these myself this summer and it would be super cool if it could be done over summer break.
Best explanation you made my day 🙏
Incredibly helpful, thank you. We are learning this in my electrical engineering class, and you explain it so well.
Sir you are the best. Other teachers just explain the topic but you have tell the logic behind it excellent sir
I was 12 years old with a Timex-Sinclair 1000 which did BASIC and assembly. I had problems understanding the binary math, but assembly was fun. I dabbled with assembly again a few years ago, which really clears up some subtle things about C programming. I still found the complements thing confusing. This video put things together in just the right way, explaining the three ways. It all makes sense now.
You make very good videos. Thank you.
Dear Ben, I did my Electronics degree in 1975 in Bolton UK and the instructor we had was from industry. Meaning? he was just like you...Absolutely brilliant. Thanks Ben you are an inspiration on how to tutor NOT teach !!!. By the way you sound just like my hero Roger McGuinn 😅
finally! someone who can teach me logic and doesn't have a heavy accent!
Simple trick for converting to two's complement: going from right to left copy all the values until you encounter the first 1. Copy it too. After that, flip the values. Take 4: 0100 ... copy all the values up until the first 1 including that first 1: 100 then flip the bits after it: 1100
You can do a similar process in *any* base, especially decimal (10's compliment)
e.g. 128 -63 = 128+(-63) ⇢ 0128 + 9936 (+1) = 0128+9937=(1)0065 ⇢ 65
so this is not a trick (like some students seem to think) it has sound basis in arithmetic. ✍
Oh, shit. I have a confusion here. When a question comes like this:
Q) Preform (4)*(-3) using Booth’s algorithm.
What will be the binary value of -3?
Shall I take the value of "signs bit", "complements bit", or "2's complement" bit?
Seemed way over my head until you said "It's not a sign bit, bit's a -8 place. BOOM, click.
Been watching your vids for years, been a programmer since you made this video. It was driving me WILD getting hung up on this little bit (lol, pun), in trying to get my degree now.
Thanks!