Hey guys welcome back to logical redstone today we’re going to be talking about subtraction subtraction is really interesting because you don’t have to build a dedicated subtractor you can actually just use an adder for example if you want to do 7 minus 3 well you can Actually just add 7 plus negative 3 and it’ll give you the same thing or if you wanna do four minus eight you can just do four plus negative eight that way we can do any subtraction problem we want using the adder that we already built in the addition episode when dealing with This representation it’s important to know how many bits you’re working with you have to work with the same amount of bits throughout the entire problem otherwise it won’t work so for the rest of this video i’m gonna be using four bits of binary the techniques i’m about To show you will still work with any amount of bits you just have to be consistent the black wool is gonna be zero and the white wool is gonna be one let’s start with positive representation first to say that a number is a positive number i’m going to use a special bit Out front called the sign bit so this bit right here which is usually an 8 because 1 2 4 8 it’s not an 8 anymore it’s a sign bit when our sign bit is zero that means that our number is positive and then the remaining three bits are just whatever number we have For example positive five is just zero one zero one because we have a zero for the sign bit and then one zero one to represent our 5. or for example positive 3 is just a 0 for the sign bit and 3 so we have positive 3. all right so positive numbers are Just a 0 followed by that number pretty easy i’ve got a little picture here this is positive 5 positive 3 and positive 7. so if positive numbers are just a 0 followed by that number does that mean negative numbers are a 1 followed by that number no absolutely Not unfortunately negative numbers are a bit weirder to introduce negative numbers to you i’m going to define a new operation we’re going to say that this operation multiplies any number by negative 1. for example if you take positive 5 and apply this operation to it it’ll give you the representation for Negative 5 because 5 times negative one is negative five but what is this operation well it has two simple steps step one is to invert all the bits which means to make all the zeros ones and all the ones zeros step two is add one to the number and that’s it that’s the Operation that will act as multiplying by negative one so using our knowledge of positive numbers and our new operation let’s try to figure out what negative five is we know that positive five is just a zero out front and five and to get negative five we can now just Apply the operation to it we invert all the bits and then we add 1 to it and that’s it 1 0 1 1 is the representation for negative five it doesn’t look like a five anymore but that’s completely normal in fact it kind of looks like Negative three because we have a one out front and then a three but that’s just not how you read this this representation means negative five and i’ve got a little drawing over here to show negative numbers as well so this is negative five negative three and negative seven again These green numbers don’t look like the actual number at all that’s just not how it works anymore but these specific ones and zeros are the representations for the numbers so one one zero one is negative three it doesn’t look like it but that’s what negative three is One zero zero one is negative 7. now that we know 1 0 1 1 is negative 5 what happens if we apply the operation again well negative 5 times negative 1 should just come back to 5 right so let’s try it out we have negative 5 which is 1 zero one one We invert all the bits and then we add one to it and look at that we’re back to positive five and that’s pretty much all i can say about representation positive numbers are just a zero followed by that number and you can use the operation of Inverting all the bits and adding one to multiply by negative one negative numbers are a one followed by some garbage that you can’t really read but if you apply that operation again you can just get the positive version back which is a lot more readable okay let’s Do some subtraction now this is a 4-bit cca adder it’s the same design as the sync edition episode and then i put red wool on the top bits because those are going to be the sign bits and green on the rest of the bits let’s start with Positive plus positive let’s do two plus three two is just a zero followed by a two and three is just a zero followed by a three so positive two plus positive three is positive five now one thing to mention is that this adder does have a limit for example if you do positive Five plus positive three instead we want positive eight but this isn’t positive eight this is a one on the sign bit which means it’s negative in fact we don’t even have enough bits to represent positive eight because positive eight would look like zero and then eight That’s five bits long so the maximum positive number we can get out of this adder is going to be 7 because 7 is a positive sign bit followed by all ones now let’s do a positive and a negative i’ll do positive 3 plus negative 2 which Is the same thing as 3 minus 2. positive 3 is 0 and a 3 and then we want negative 2. so i’m going to put in positive 2 first and then i’m going to apply the operation we’re going to invert all the bits and then add 1 to it Three plus negative two is positive one in other words three minus two is one let’s do one minus four positive one and then to make negative four you start out with positive 4 you invert all the bits and then you add 1 to it 1 minus 4 is negative 3 and this is Negative 3. i know it doesn’t look like it negative numbers are just weird and not very readable but this is negative three you can even look at the uh the chart over here one one zero one now if you were debugging and you wanted to prove to yourself that this was in fact Negative three what you could do is you could invert all the bits and add one again and see if you get positive three and so if we do that if we take all of these bits invert them and add one yeah we got positive three so that’s totally correct this right here is Negative three all right so let’s turn this adder into a subtractor instead of doing a plus b we wanted to do a plus negative b so let’s just invert and add one on the b side to invert you can just move these back and add a torch and then to add one Just use the carry in and just like that that’s our subtractor that’s it if you want to do five minus three you can do five minus three which is positive two if you wanna do one minus seven you get negative six and by the way the term for this representation is called Two’s complement it’s really really common in computers it’s what most computers use to represent positive and negative numbers if you’re interested in the theory behind it and like why it works i’ll link a video in the description for that that goes over it in more detail the other cool thing you Can do is instead of making a permanent subtractor like we just did you can make a toggleable subtractor so when this lever is off or pointed up this is just an adder 3 plus 2 is 5. but when this lever is pointed down or on it turns it into a subtractor so five Minus one is four and the way that we do that is actually pretty simple these purple circuits which you might recognize are a bunch of xor gates the b number on the right is going into one input of the xor gates and then the lever is controlling The right input of all the xor gates this is a really neat trick using xor gates which allows you to optionally invert your input when the right input of the xor is off it doesn’t do anything to it because that’s how the truth table of an xor works I mean 1 and 0 gives 1 and 0 and 0 gives 0. so it doesn’t do anything to this but when the right input of the xor gate is on it’s inverting it because one and zero gives one and one and one gives zero and if you Just look at these lamps it’s acting like a torch so when you turn this on and flip it into a subtractor literally all it does is powers the sides of all these xor gates which we now know inverts b and it adds one by powering the carry in so yeah super Cool stuff i mean we just added the option for subtraction with this purple circuit on the saturn and now it can be used for either one and that’s all i got for you guys i hope you enjoyed i hope you learned something peace out you Video Information
This video, titled ‘Subtraction | Logical Redstone #12’, was uploaded by mattbatwings on 2022-01-12 15:30:50. It has garnered 28496 views and 1148 likes. The duration of the video is 00:08:57 or 537 seconds.
!!! Check out the NEW AND IMPROVED logical redstone series here! https://youtube.com/playlist?list=PL5LiOvrbVo8keeEWRZVaHfprU4zQTCsV4 !!!
In this episode, I cover 2s complement representation and how to build a subtractor.
Patreon: https://www.patreon.com/mattbatwings Discord: https://discord.gg/V5KFaF63mV My socials: https://linktr.ee/mattbatwings My texture pack: https://github.com/Xyverle/MattPack World Download: (JAVA 1.17.1) https://www.planetminecraft.com/project/subtraction-from-tutorial-video/
More 2s comp: https://youtu.be/4qH4unVtJkE
————————-
Want to get more involved in the logical redstone community? Learn Logical Redstone! https://youtube.com/playlist?list=PL5LiOvrbVo8keeEWRZVaHfprU4zQTCsV4 Open Redstone Engineers (ORE): https://openredstone.org/
0:00 Subtraction overview 0:43 Positive representation 1:30 Negative representation 4:02 Subtraction Examples 6:21 Building a Subtractor 7:14 Building a Toggleable Subtractor 8:42 Subscribe!
Music: https://youtu.be/mWebB6zby6Y