Want to conquer negative numbers in binary? This fun, beginner-friendly tutorial breaks down two’s complement with step-by-step examples (-109, -29) and shows how to convert, pad, and subtract in binary. Learn sign bits, carry bits, and avoid common mistakes. Subscribe for more coding tips and tech tutorials that make learning a blast! Scan the QR code for more resources and join our community!
Introduction to Two’s Complement 00:00:00
Signed vs. Unsigned Integers 00:00:28
Sign Bit Explanation 00:01:55
Positive and Negative Representation 00:02:06
Range of Signed Integers 00:02:48
Padding Signed Integers 00:05:36
Converting to Negative (Example: -109) 00:07:00
Binary Addition and Carry Bits 00:10:16
Correcting Conversion Mistakes 00:16:38
Converting Negative 29 00:13:32
Subtraction Using Two’s Complement 00:18:21
Adding Binary Numbers (109 – 29) 00:20:41
Verifying Results 00:23:56
Conclusion and Call to Action 00:25:40
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
Please help support us!
- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main “pinned” video of this channel for offers and extras
Hey there! Let’s talk about representing negative numbers in binary.
We’re going to use a system called 2’s complement which is going to allow us to represent signed integers,
which means there will be a positive sign or a negative sign on the integer
and we’ll still be able to do it in pure binary.
So let me talk a little bit about what I mean first here.
signed and unsigned okay so if i just type the number 14 by itself that is uh that’s an unsigned
integer we don’t really know if it’s positive or negative like do we know that it’s negative 14 or
do we know that it’s positive 14 right so the sign is what lets us know if something is positive or
negative okay so if i if i type a number in binary let’s just do a pure binary number one two three
if you know how to convert binary to decimal you probably recognize right away that this is just
the number zero even if we add a couple of bits here that’s just like the number three
and so this is the number three but so far if if you haven’t learned signed integers in twos
complement or in binary then you don’t really know that there’s a sign you just kind of assume
that the number is positive by default if you’re not using twos complement and you’re just saying
and you’re just saying like well let’s just do a bunch of binary you know digits then yeah it’s a
safe assumption that the the sign is positive but we’ll use two’s complement which starts with the
idea that the highest bit the leftmost bit the bit with the height you know the most power that’s
going to turn into the sign that’s that’s going to turn into the plus or the minus so like you
the sign.
So this means that we have to decide, you know, does a zero mean positive or does it
mean negative or what means what?
So in two’s complement, we’ll say that zero is positive and one is negative.
So you can see right here that we’re still actually representing the number three, but
it’s positive three for sure because this sign bit right here is zero.
That’s positive.
Once we decide to represent two’s complement, then we can say that for sure.
On the other hand, if we put a one here,
then we definitely know that this number is now negative.
However, we don’t actually know that it’s a three
because the numbers don’t work out the same anymore
in two’s complement.
So the positive numbers, they will mostly look the same.
The negative numbers, they’ll look a lot different,
but they’ll still be valid in order,
you know, in terms of us being able to add them together,
subtract them from each other and things like that.
So two’s complement is pretty cool.
Let’s do, let’s see, what else can I tell you?
Oh, one thing to understand is that
One thing to understand is that in an 8-bit unsigned integer in binary, let’s say 1, 2,
3, 4, 5, 6, 7, 8, let’s say unsigned bits, maybe I’ll just put like, maybe just some
like V for value, all the bits are values, that means the range is 0 to 255 with 256
total combinations.
But if we want to use a signed number, then we’ll actually have the signed bit be the first bit,
and all the rest will be value bits.
1, 2, 3, 4, 5, 6, 7.
In this case, the range kind of goes down.
Because if you think about it, every single bit, you know, represents like, you know,
it helps towards the highest possible value that you can represent.
If we’re using the leftmost bit, then for an 8-bit integer, that’s 128.
integer that’s 128 that’s a value of 128 so we lose a lot off of the maximum
integer that we can represent so the range here is I think it’s negative 20
128 to positive 127 just keep in mind you’ll have to trust me in terms of why
is the negative 128 bigger and the positive 127 you know smaller but that’s
just the way it is so that means we only get these value bits here and if you if
that um let’s see if i can do this quickly enough without screwing it up we’ll say 127 minus um
minus negative 128 i guess that’s 255 and then also a zero but uh in terms of positive and
negative the way this is going to work out is the zero will probably show up twice because uh
255 possible combinations with seven bits.
Normally you would have a range from negative, sorry, from zero to positive 255.
And in this case, we’re just, you know, losing our range, but we can represent negative numbers.
Anyway. Okay.
So let’s talk about doing an example real fast. Let’s see.
I’ve got like a little notes to tell me what I should do. Okay.
It’s important to understand that normally when you have, let’s say, let’s say you have
like an 8-bit number and you go 1, 2, 3, 4, 5, 6, 7, 8.
And then you have 2 bits right there and so this is like, you know, positive 3.
If you wanted to copy that number into more bytes, like for example, if you wanted to
take a 2-byte integer and have it copy the value of a 1-byte integer, then it’s pretty
easy.
zeros to the left you’ll say one two three four five six seven eight that would work perfectly
however if you did this with a negative number let’s say that we have a negative number
I’m going to put some of the random patterns so that you don’t think it’s the three let’s see
one two three four five six seven eight that’s eight total so I’m going to make that a one and
then get rid of that so we have like eight negative number if we were going to copy that
integer then we would copy paste it to start but then you’d have to pad with
ones to the left one two three four five six seven eight same negative number
more bits so be very careful about how you pad if you’re padding an unsigned
integer then yeah you’ll always pad zeros to the left no matter what is
happening but if you’re padding a signed integer then you have to pad differently
signed integer you have to pad with whatever the highest bit is so in this case the highest bit was
a zero so we do pad with zeros but then in this case the highest bit was a one so we have to pad
with ones if you don’t do that you’re going to end up with a number that doesn’t actually make
sense okay so now let’s work on actually converting a number to a negative number or representing a
negative number in twos compliment okay so i’m going to write twos compliment here and then uh
Let’s start off with the number negative 109.
Okay, how do we do this?
The first thing is convert it to its positive form.
Take the absolute value. Okay.
So we’re really just, you know, take positive form here,
and that’s just going to be positive 109.
Okay, no problem.
I’ll say start with negative 109, and then we’ll take the positive form 109.
Now we’ll convert it to binary.
Okay, this is not a video that teaches you how to convert to binary, so I’m just going
to try to do this in my head real fast here.
It’s going to be, let’s see, we got 8 bits, we’ll use just 8 bits to store the number,
and because it’s low enough and I don’t want to use that many bits, so it’s odd, so I can
add a 1 there, but maybe for now I should find the highest bit that is less than the
actual number.
So this is the 128 bit right here. So that’s going to be a zero. This is the 64 bits
I’m going to put a one there and I’m just going to say
Mmm, maybe like
This is like maybe not the smartest way to do it. I’ll say 109 minus 64
Because I put a one there and then 45 so now this is 32 that’s less than 45. So I’ll put a one there and
Then it’s going to be minus 40
So it goes from 45 to 13.
So then that was the 32.
Okay, so 128, 64, 32, and then 16.
Is 16 less than 13?
No, it’s not.
So we’ll put a zero here.
And then the next one is going to be just four.
Four is definitely less.
So I’ll put a one bit there.
And then I’ll subtract four.
And then one, two, four.
Oh, wait a minute.
One, two, four, eight.
wait a minute one two four eight sorry that was supposed to be subtracted uh subtracting eight
because that was the eight bit uh then i want to get five so here is the four bit so i put a one
there and then i’ll just put a one here because four plus five is equal to uh i’m sorry four plus
one is equal to five so let’s see um it is let me just double check here one
one let me just double check my conversion real fast it’s going to be
the one bit plus two four plus eight six thirty two plus thirty two plus sixty
four did I get one on nine yeah okay so I guess I did it right so convert to
binary and that’s going to be this the next thing we’ll do is we will invert the
bits I made a little edit jump here because I inverted the bits incorrectly
on the first try which is sad but hey it happens the next thing we’ll do is
but hey it happens the next thing we’ll do is we’ll invert the bits so basically you know we
take this original sequence here and i’ll just turn every single bit uh i’ll flip it i’ll flip
ones to zeros and zeros to ones so i’m going to go one zero zero one zero zero one zero okay so now
we have this number invert the bits then we just have to add one so we’ll add positive one to that
that it’s just going to be a one there at the end.
But sometimes that might not be the case
because what if we already had a one there
and we had to add one to that?
Well, we’d add one to the right side
and it would turn into a zero
and then it would carry a bit to the left.
That would become a zero
and then the carry bit would show up all the way over there.
So I just want you to be aware of the fact that
when you add two binary numbers together,
you have to be careful.
You have to add them the same way you would add decimal numbers.
For each digit’s position,
you have to add the two numbers together and then if they overflow then you just
kind of wrap around to the lowest number again subtracting you know the highest
value or sorry subtracting the base like in decimal if you add 9 and 9 the answer
is 18 but you’re not going to write 18 in that one position you’re going to
subtract the base which is 10 so it’ll actually be 8 and then you’ll carry the
plus nine is equal to eight carry the one right so we’ll do the same thing in binary we’ll say
if we ever get a one plus one when we’re adding the answer will be two but then we subtract the
base which is two so the answer is actually zero carry a one bit keep that in mind i’m just gonna
write it all out for you so you can kind of get a little bit of practice it’s important to to
practice this because it’s easy to get wrong i’m going to put a bunch of dashes up at the top to
and I’m going to say we’re going to add, you know, one number plus another number,
put a little plus symbol over there just to try and make sure we do it the right way.
Okay, so how do we add these?
Well, we’ll just go to the right side.
Zero plus one is one, no carry bit.
One plus zero is one, no carry bit.
Then we got a couple zeros here, no carry bit, of course.
One plus zero is one, no carry bit.
And then a couple zeros and then another one with no carry bit.
Okay, so, you know, we could have done that pretty easily, but
but well now we’re getting a taste for binary addition that might be harder later.
So we’ll just do that for now.
And now this is the two’s complement representation of negative 109.
I’ll say now we have negative 109 is equal to that.
leftmost number the most powerful sorry leftmost bit the most powerful bit is a one remember one
always indicates negative so when you’re looking at it if your leftmost bit turns out to be a zero
then you probably did something wrong or you had an overflow maybe um and again if we were going
to try to you know send this number into a two byte number or an eight byte number or whatever
uh then we would just have to pad with the sign bit so one two three four five six seven eight
help my brain so this was this is the way it would look in a two-byte number this is the way it would
look in a three-byte a four-byte and five six seven how many one two three four five six seven
okay one more this is what it would look like as an eight-byte number or a quad word 64-bit number
yeah okay so now we know how to do negative 109 okay so now let’s do a number that’s a little bit
negative 29. So let’s say convert, or how about represent negative 29 into
2’s complement. Okay. So first we, you know, first get the absolute value.
So just 29. And then we have to invert the, sorry, we have to get that into binary. So
next convert to binary. And I’m going to start with zeros, one, two, three, four, five, six,
1, 2, 3, 4, 5, 6, 7, 8.
For this video, remember, we’re choosing to use one byte integers.
But if you wanted to do a bigger one or you had to do a bigger one,
then just, you know, keep that in mind.
Okay, so 128 is not smaller than 29.
64 is not.
32 is not.
16 is, though.
So I’ll put a 16 bit there.
And I’ll just subtract 16 from 29.
29 minus 16.
Now we’ve got 13 left.
So 64, 32, 16.
six four thirty two sixteen eight okay so now I’m gonna put a one bit there and
I’m gonna subtract 8 from the remainder and then we got a five which is pretty
easy to do eight four and then a one so now we have well zero zero zero one one
one zero one that’s the binary number the positive or unsigned representation
representation. So now we’ll add one. Positive one. And let’s try to do this the right way so
that we can practice carry bits with addition a little bit. Notice how this one is already there
on the right side. So it’s going to, we’re going to have at least one carry bit for sure.
So then I’m going to go doop like that. And then I’m going to say that we have like, you know,
what is the result? Put a positive sign there. And then I’m going to put a bunch of dashes
my carry bits because I can I can forget that pretty easily. So the first thing is we add one
and one. The answer is two, but we can’t put the number two here because it’s binary. Instead we
need to subtract the base which is two. So two minus two is equal to zero, but then we have a
carry bit of one. So I’m going to put a one there. The first carry bit will stay as a dash for this
whole you know exercise because you’re not going to carry on to the first digit. So now we have
what would have been just zero plus zero now we have one plus zero plus zero so
that means this is going to be one and then the carry bit is just going to be
zero because we don’t actually carry anything so then we have zero plus one
plus zero so that’s going to be a one and then zero carry bit and then zero
one zero is just going to be one and then there’s going to be no carry bit
and then zero one zero again a one no carry bit because we didn’t actually
zero zero zero and then I’m just gonna put zeros here okay so now we have
successfully added I blew it totally blew it I always forget steps don’t
forget the steps this is a good lesson I’m gonna leave this in the video
because I want you to see that everybody makes mistakes and you got to practice
practice practice especially before you have to actually do this in real life or
or something like that.
Next, convert to binary.
Before you add one, I’m gonna just remove this.
Oh my gosh.
Next, flip the bits,
which is gonna be 1110010.
Okay.
So now we take this bit flipped number
and we will add one.
So that’s gonna be zero, zero, zero, zero, zero, zero, zero,
one, oh, it’s too easy.
Maybe I got excited and I thought,
excited and I thought, oh, it’s carry bit time.
But even though the last edition that I did was wrong because I forgot to carry
the or flip the bits, you still at least saw a little bit about how to carry the bits.
Right. OK, so it’s just going to be one.
Let me start from the right side.
One one zero zero zero one one one one one one.
Let me just double check here.
One one one zero zero zero.
OK, so I got that.
Now we have.
negative 29 in twos compliment. Again notice if we actually tried to add those numbers up to be
like an unsigned binary number they’re not really going to make sense because this is like 64 plus
32 plus 3. So what would that end up being? Let’s just double check here. 64 plus 32 plus 3, 99.
That’s not actually the number but the number is 29. So keep in mind you can’t just look at this
unless you’re like really, really practiced.
Okay, and again, notice that the number is 1 at the very left,
indicating that it’s a negative number.
Now, let’s look at how to subtract one number from another
using 2’s complement.
Okay, so what I want to do is I want to subtract,
let’s say, 29 from 109.
Okay, so let’s subtract, and I’ll just say 109 minus 29.
109 minus 29.
And how I would do that is basically I’ll start by just taking 109.
Let’s see, convert to binary.
So I’m just going to copy paste that number.
So 109 is this and then 29, take the positive version.
It is just, let’s see, before we flip the bits or anything,
let me make sure that I grab the right one.
Invert the bits, okay.
So positive 29 is this number.
Whoops.
is this number whoops let me put parentheses around that so it’s easy to tell and then I’ll
put a positive sign there like that maybe like that nope nope nope nope how about this okay
so now we have both of these numbers in positive form so now if we added 109 plus 29 that wouldn’t
negative positive 29 that would be what we wanted right because really if you’re subtracting
i’ll say aka
positive 29 plus negative 29 all we really need to do is um invert the 29 and then add the result
to 109 so that means we’ll we’ll turn positive 29 into negative 29 using two’s complement
through the steps again but basically put it there but basically you know that’s negative 29
so say positive 129 is equal to what i just put up here and then negative 29 is equal to
maybe i should do the parentheses again for clarity uh is equal to this okay so you can tell
that positive 29 is pretty pretty different from negative 29 but now we have
29 but now we have both of those numbers so let’s see 0 1 1 0 and then we’re ready to add okay all
we got to do is add them together next add them together maybe I should write the steps up here
negative 29 using twos complement and then next add them together so then I’m
going to copy paste the bits here and it’s going to be this plus this do a
positive plus sign just to remind ourselves that we are actually adding
and then I’m going to put a bunch of placeholders for sign bits up at the
top and now we’ll have a little bit more fun adding numbers together maybe I’ll
drag this down
Oh my god. Oh, there we go. Okay. So I’ll start with the one on the right, the position on the
right. That’s going to be one plus one equals two, but then that’s an overflow. So I’m going to
subtract the base. So it’s going to be zero. And then don’t forget to carry the one. Oh,
cool. More interesting. So we have one plus zero plus one. That’s going to be another two carry
two carry the one so it’s going to be zero and then carry the one again so i’m going to put the
carry bit up there and then again we have one plus one is equal to two so it’s going to be zero
carry the one again zero carry the one and then finally we don’t really have a carry bit
um so we’ll just have like a one and there’s there’s no carry so it’s going to be you know
carry a zero and then we add these two together so it’s going to be a zero carry the one and then
Now we have a three.
Oh, that’s kind of nice.
So this is an interesting edge case kind of.
One plus one plus one is three.
But if we subtract two, the base from it, you know, three minus two, it’s going to be one, not zero.
So it actually is going to be a one and then carry the one on top of that.
Then for here, let me space this over a little bit so that I can illustrate what’s going on a little bit better.
We’re going to have one plus zero plus one.
definitely going to be zero and then carry the one but there’s no bit where that carried one can
can show up on right so that one overflows it falls off the edge if this was a bigger number
then okay we you know if we had more bits to this number then sure we would just keep carrying over
over to the left but remember we said before that when we have a very big number let’s see
bits it’s just ones all the way to the side that will actually help us make sure that if our final
number is actually going to end up being positive that everything kind of like dominoes like carry
the one carry the one carry the one carry the one carry the one all the way until one of the ones
falls off think about it so anyway this one just is gone we don’t really care about it anymore
the result is going to be just only eight bits because that’s the number that we started with
bunch of zeros what is you know the final answer let’s just compute this
real fast to decimal so this is 128 and then 64 so it’s gonna be 64 plus not 32
but 16 so 64 plus 16 that’s gonna be oops 16 that’s gonna be 80 and now we
just have to ask ourselves again as like a final step to double check yourself to
what you’re doing and that you got it right is just punch up 109 minus 29 just to make sure
109 minus 29 whoops what happened here 109 minus
oh i think i stole my subtraction key for the annotator 109 minus 29 is 80
so again you know if you’re if you’re trying to like you know write something down to do some
you know taking an exam or something you definitely want to double check yourself in several ways
As you can tell from this video alone
I got one of these things wrong because I forgot to input the bits as a step before adding one
So you know your final step should be actually trying to add two numbers together or subtract numbers or whatever
You’re doing to make sure that you got the binary correct
So let’s see
Hmm
I guess maybe your first indication that the result was going to be positive would be that
there’s a zero there. And just, you know, as a sanity check, you look at the top and you’re like,
well, I was going to subtract a small number from a larger number. So the result should probably be
positive, right? Like 29 is like way lower than 109. So it should be positive, which means the
final result should have a zero at that leftmost position. Okay, so that’s two’s compliment,
two’s complement how to convert numbers from positive to negative in two’s complement you
know what the sign bit means and all that stuff and how to perform subtraction via two’s complement
i hope you enjoyed this video thank you for watching i hope you learned a little bit of
stuff and had a little bit of fun see you in the next video hey everybody thanks for watching this
video again from the bottom of my heart i really appreciate it i do hope you did learn something
If you could do me a please, a small little favor, could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you’re looking at right now.
It would really mean the world to me and it’ll help make more videos and grow this community.
So we’ll be able to do more videos, longer videos, better videos, or just I’ll be able to keep making videos in general.
So please do me a kindness and subscribe.
in the middle of the night and I just wake up because I know somebody
subscribed or followed. It just wakes me up and I get filled with joy. That’s
exactly what happens every single time. So you could do it as a nice favor to me
or you could you could troll me if you want to just wake me up in the middle
of the night just subscribe and then I’ll just wake up. I promise that’s what
will happen. Also if you look at the middle of the screen right now you
should see a QR code which you can scan in order to go to the website which I
think is also named somewhere at the bottom of this video and it’ll take you
you to my main website where you can just kind of like see all the videos I published and the
services and tutorials and things that I offer and all that good stuff and if you have a suggestion
for clarifications or errata or just future videos that you want to see please leave a comment or if
you just want to say hey what’s up what’s going on you know just send me a comment whatever I also
wake up for those in the middle of the night I get I wake up in a cold sweat and I’m like
it would really mean the world to me. I would really appreciate it. So again,
thank you so much for watching this video and enjoy the cool music as I fade
into the darkness, which is coming for us all.
Thank you.