Beginner Electronics - Beginner Electronics – 28 – Binary Arithmetic & 2’s Complement

Electronics, Howto & Style

Beginner Electronics

29 Lessons

Beginner Electronics – 28 – Binary Arithmetic & 2’s Complement

what is going on everyone my name is Kota moore and welcome back to electronics episode 28 in this episode we are going to learn how to add binary

numbers together as well as something called two's complement and two's complement lets us represent negative numbers in binary and in turn allows us to subtract binary numbers now this is

really important because in the next two videos we are going to be designing and creating a binary adder and that is going to be the very first part of our 8-bit computer so let's get on to simple

binary addition really if you can add 1 plus 1 in binary you'll be able to add any number in binary so let's do that let's take 1 plus 1 in binary now in decimal this is of course equal to 2 in

base 10 which is as we can easily determine 1 0 in binary so if we can memorize that 1 plus 1 in binary equals 1 0 we can take this principle and apply it to a larger binary number let's do

something like 5 plus 2 which should equal 7 in base 10 so how do we represent 5 in binary well that's just 1 0 1 and we are going to add that to 2 so what is 2 in binary well that's just 0 1

0 so let's go ahead and add these numbers together now we can actually use a system that might be taught to little kids as they learn to add base 10 numbers when they're young all we have

to do is add column by column starting at the far right at the least significant digit of each number so we are going to start by looking at this column right here well what's one plus 0

in binary that's very simple that's just 1 because 0 added to anything is just itself great now we can move on to the second column over here again we have 0 plus 1 in binary which is just 1 again

ok that's simple and we'll take the next column over again 1 plus 0 that equals 1 in binary now if we convert this number from binary base 2 into decimal we indeed get the number 7 which is exactly

what we were expecting so let's try something a little bit harder so let's do something very similar let's do 5 plus 3 this tie which should equal eight in base 10 so

again we already know that five and binary is one zero one and we can determine that three in binary is zero one one and we're gonna take these two numbers and we are going to add them

together so just like before we're gonna take a look at this first column over here and we'll see that we instantly have a problem we have a one plus one now we know one plus one equals two

which is equivalent to one zero in binary base 2 but we can't fit one zero into a single digit space of our final result so instead we're gonna take the rightmost digit which is a zero in the

case of it being two and we're gonna move that digit down underneath the first column but remember we still have this one to work with so that number we are just going to tack on to this column

over here the next column to the left so now we can move on over to adding up the left column here the the second column over again here we have one plus zero plus one which again equals 2 or 1 zero

in binary so I'm gonna bring the zero down just as I did in the first column and that extra one is gonna be tacked on to the next column over and now if we look at this column we can see that we

have one plus one plus zero which again is two so we'll bring the zero down and we get an extra one on the next column over well now we just have a 1 over here in this fourth column there's nothing

else to add it to so that's just gonna stay a 1 and you'll notice that one zero zero zero in binary base 2 is equal to 8 in base 10 so we got the right answer all right great let's do something even

more tricky then let's go ahead and do 3 plus 3 which of course should equal 6 in base 10 so 3 in binary is again 0 1 1 actually we don't even need that as leading zero so it's just 1 1 and we're

just adding that to 3 again so plus another 1 1 all right so let's continue just like we did before I'm gonna take this first column and we that we have two ones that equals a 2 or

a 1 0 so we're gonna bring the 0 down and we're gonna add the 1 on to the next column well now we have something completely different now we have three ones to add together we know that one

plus one is two so one plus one plus one is three and well if you haven't guessed it already three in binary is just one one okay so what do we do here well same exact thing

as if we had added two ones together we're gonna take the rightmost binary digit and we're just gonna move that digit down so this is gonna stay a 1 and we're gonna take the left digit and

we're gonna move that over to the next column over of course this third column has nothing else to add it to so we can move this one down as well so we get 1 1 0 in binary equals 6 in base 10 the

correct answer so that's all I'm gonna show about binary addition if you want more practice with this or you want more or you want more info on this there are plenty of YouTube videos dedicated to

explaining binary maths and stuff as well as articles online let's take a very brief break from this binary math stuff and let's quickly describe how a computer stores these binary numbers

because that's going to be important for when we learn two's complement in just a second so for example our 8-bit computer is going to store all of our numbers in chunks of eight binary digits and I'm

gonna call these eight bits so this is either gonna be referred to as eight bits or eight binary digits either a 1 or a 0 or you might also refer to this as a single byte so a single byte is 8

binary digits and our 8-bit computer is going to store numbers all these integers positive and negative numbers in groups of eight binary digits and groups of bytes now if you had a 32-bit

computer at home it's gonna be storing all these numbers in groups of 32 bits per group if you had a 64-bit computer then every number is gonna be made up of 64 bits and grouped that way instead so

the size that we're able to store data at is gonna be really important because we have to know how to represent negative numbers as well as positive numbers so if you were just learning

binary you might initially think that representing a negative number is gonna be really really simple in binary and let me show you what I first thought when I learned this let's say we are

working with a 4-bit wide so we have 4 available binary digits rather than 8 just to make it easier so we have our numbers are able to go four bits wide our computer is grouping them like that

well let's take the number I don't know one so one is going to be zero zero zero one which is just one in base 10 what if we wanted to get negative one in base ten what is that going to equal well

what we could say and I'm not saying that we should do it this way but what we could say is let's take the farthest left bit available in our computer in our grouping of these numbers and let's

make that the sign bit so we can say if this furthest bit is a zero then we're going to have a positive number and if it's a one then we're gonna have a negative number so if we wanted to

represent negative one we just have to set that bit to zero so we get one zero zero one and we could say that that is negative one okay great so now let's go ahead and try adding one plus negative

one which of course we know should be zero well let's do this in binary so we have our representation of one and we're gonna add on our representation of negative one right so we're adding one

plus negative one well if we do the addition like we know how to do it we're going to have one plus one is two so we're going to bring the zero down and carry the 1 1 plus 0 plus 0 is 1 0 plus

0 is 0 0 plus 1 is 1 ok that does not look like zero at all in fact if we take a look at these three bits that is equivalent to two and we know that since this bit means negative

if it's one well we have a negative bit here so this must be negative two and let me tell you zero does not equal negative two so there is an even better way to represent negative numbers in

binary especially when we want to add and subtract these binary numbers nice and easy and when we want to build a simple circuit to do that for us fast and efficiently and that is called two's

complement so when we're talking about two's complement here we're gonna do something special to make a number negative let's take our representation of one again so this is one in base 10

to calculate the two's complement we're gonna do a two step process first we are going to flip every single bit so if we have a zero we're gonna change it to a one so that's gonna become a one that's

gonna become one and this is gonna become a one this one here is gonna become a zero and after we've flipped all the bits we are going to add one to the answer so we are going to add a

binary one to this so looking at only the green here we have 0 plus 1 is 1 1 plus nothing is 1 1 plus nothing is 1 1 plus nothing is 1 and we are going to say that this is equal to negative 1 in

our base 10 system now this is really kind of strange and not intuitive at all especially when you get into larger numbers it's really hard especially for me to determine if a number is negative

15 or negative 18 or positive 18 anything like that it can be kind of difficult but I will guarantee you that we are better off doing it this way because we can make a circuit to do all

this really really easily and why do I say that well let's do the same exact example let's take our representation of 1 and add it to negative 1 so we're gonna have

1 plus negative 1 now we know that in two's complement negative 1 means all one's here so we're gonna add 1 1 one two this let's go ahead and add this together one plus one is two so we're

going to have a 0 and carry the 1 1 plus 0 plus 1 is 2 so 0 carry the 1 same thing 0 carry the 1 1 plus 0 plus 1 is 0 carry the 1 now you can see that we get an answer of 0 so 1 plus negative 1

is now equal to 0 that's really nice this means we can do binary addition and since we can make negative numbers we can do subtraction just using addition and making a number negative all right

now you might have noticed that something interesting happened here we do have this extra one that got carried so technically our number should really be 1 0 0 0 0 but in this example I said

that our computer is only going to support 4 bits at a time for binary digits now this means that we have to be careful when we do adding and subtracting because we might add a

number that gets too big for our 4 bits to store now the reason why it's okay for us to just throw out this bit in this case is because this carry bit and the carry bit before it are the exact

same if these were different than we would have had a problem and let me show you that right now let's do I don't know 6 plus 5 a nice easy addition problem what is 6 in

binary well it's going to be 0 1 1 0 and then we're going to add that to 5 which is 0 1 0 1 ok let's go ahead and do this we're gonna have 0 plus 1 equals 1 1 plus 0 equals 1 1 plus 1 equals 2 so

we'll bring down the 0 and carry the 1 and now we have 1 plus 0 plus 0 is a 1 now we know that 6 plus 5 should equal 11 and this in binary does equal 11 but there's an issue here and that's because

this is actually in two's complement the negative representation of another number if we flip all of these bits so we get zero one zero zero and then we add one

to it we get zero one zero one this here one zero one one was actually a representation for the value of five or rather negative 5 in this case up here and that's because we can only store

values from negative 8 all the way up to positive 7 so 11 isn't even representable using this two's complement system with only 4 bits now how can we check for if this

was an invalid result for the amount of bits that we use well all we have to do is compare the carry bit of the last column with the carry bit of the next column over now in this case there was

no extra bit here so we can assume it was a 0 since these 2 bits were not equal they were both different one was a zero and one was a 1 we know that our answer is in valid it isn't actually

equal to what we thought it would be we wanted to get 11 but 11 can't be represented so instead it plucked out negative 5 which at first glance looked correct to us humans but

not to the two's complement computer system so two's complement is great it makes our circuitry easy we can add we can subtract numbers now we just have to be careful to make sure that we check

these last two carry bits to make sure our result was it within their valid range for the amount of bits that we were using for those of you who want to stick around let me show you one other

example and maybe if I didn't explain something well I'll catch it this time around let's do a little bit larger of a number let's let's take a look at this binary secret sequence 1 0 1 0 0 1 so

now I'm gonna say that my computer can support 6 bits at once just for the sake of this example now luckily with two's complement I can still say that this does mean that the number is negative

it's just I can't read this as the actual value of what negative number it is so I do know that since it starts with a 1 on the far left the number must be negative so

what number is this well to get it back to a positive number I have to flip all the bits so I'm gonna inverse all the bits and then I'm gonna add one to it so we're gonna have 1 1 1 0 1 0 like so and

this if we calculate it I believe is 23 in base 10 which means this up here was negative 23 in base 10 okay okay so let's take our negative 23 and let's just I don't

know we'll add a negative 5 to it oh boy now we have to figure out what negative 5 is in two's complement with 6 bits well in order to do that let's go ahead and take our representation of 5 which

is going to be hopefully just 1 0 1 we got to flip all the bits so we're gonna get this and then we're gonna add 1 to that and when I say add 1 I'm only talking about these two rows here so 0

plus 1 is 1 and then that stays the same all the way through so this is our representation of negative 5 using the two's complement system so let's go ahead and actually calculate

this negative 23 plus negative 5 and of course we should hopefully get negative 28 out as the result so we'll grab our negative 23 so we're gonna have 1 0 1 0 0 1 and we are going to go ahead and we

will add our negative 5 which is this representation so we'll add 1 1 1 0 1 1 let's see what we get when we do this addition here so we know a 1 plus 1 is a 2 so we carry the 1 1 plus 0 plus 1 is 2

so we will carry the 1 1 plus 0 plus 0 it's just a 1 1 plus 1 is 2 so 0 and carry the 1 1 plus 0 plus 1 that's 2 so 0 and carry the 1 now we have one plus one plus one that's

three so we're gonna write a one and we're gonna bring out an extra one over here and this is kind of our extra bit again we're just gonna wind up throwing this bit away now before we look at our

answer though is this a valid answer in our six bit two's complement system well we're gonna go ahead we're gonna check the last carry a bit that we had and compare it to the one right before it

they're both the same they're both one which means our answer is okay it's completely valid now that we know that let's figure out what our answer actually was in base 10 well we know

that our answer ends with a one on the left so it is a negative number so to figure out what number is negative here we're gonna flip all the bits just like we've been doing and then we're gonna

add one we're gonna get one plus one that's zero carry the one and plus one is zero carry the one and then 1 1 1 0 and this equals thankfully 28 in base 10 meaning this must have been negative 28

in base 10 and we got the right answer that we were expecting again if you have any trouble with two's complement stuff if I maybe didn't teach in your style there are again plenty of videos out

there explaining it plenty of articles I highly encourage you to learn this stuff because it is in essence what the very beginnings of our 8-bit computer is going to be able to do so thank you all

so very much for watching and I will see you all in the next video

Today we learn binary addition and 2’s complement, which will allow us to also represent negative numbers and subtract binary numbers!

Need source code? See my website:
Follow me on Twitter – @CodeNMore –
Comment, PM, or Tweet me for help!

Teaching to understand all subjects to the fullest extent!

    2's complement 555 555 timer 8-bit addition advanced alu amazing and and gate arduino arduino uno arithmetic astable attiny attiny85 awesome base 10 base 2 battery Battery (Invention) beginner binary bipolar junction transistor bistable bjt breadboard build building button capacitance capacitor charge circuit circuits components computer Computer Science (Field Of Study) computers decimal development digital logic diode easy education electricity electronics Electronics (Field Of Study) Engineer (Profession) engineering Engineering (Industry) episode 1 episode 10 episode 11 episode 12 episode 13 episode 14 episode 15 episode 16 episode 17 episode 18 episode 19 episode 20 episode 21 episode 22 episode 23 episode 24 episode 9 Experiment explained first floating inputs full adder half adder hard hardware Help how how-to ic integrated citcuit intermediate Invention (Literature Subject) invert gate inverter learn LED Light-emitting Diode (Invention) logic gate logic gates Math measure Mechanics microcontroller monostable multimeter non latching relay not gat npn transistor ohm ohms law Open-circuit Voltage oscillator parallel part 1 part 10 part 11 part 12 part 13 part 14 part 15 part 16 part 17 part 18 part 19 part 20 part 21 part 23 part 24 plans pnp transistor potentiometer Power programming relay relays resistance resistor Robot Robots schematic schematics scratch series software speaker subtraction switch switching symbol Technology Tips transistor Tricks tutorial two's complement variable resistor Watt Wattage xor xor gate