It is not only a leading bit. This is about all the bits.
Starting from add
First, let's see how the addition is done in a 4-bit binary for 2 + 7:
10 + 111 ____ 1001
This is the same as the long addition in decimal format: bitwise, from right to left.
- In the far right place we add 0 and 1, it does 1, does not transfer.
- In the second place on the right, add 1 and 1, which makes 2 in decimal or 10 in binary format - we write 0, carry 1.
- In the third place on the right, add 1, which we moved to 1 already there, it makes binary code 10. Write 0, carry 1.
- The 1 that you just received is written in fourth place on the right.
Long subtraction
Now we know that binary is 10 + 111 = 1001, we should be able to work backwards and prove that 1001 is 10 = 111. Again, this is exactly the same as with decimal subtraction.
1001 - 10 ____ 111
Here's what we did, working from right to left again:
- In the rightmost place, 1 - 0 = 1, write down.
- Secondly, we have 0 - 1, so we need to take an extra bit. Now we make a binary 10 - 1 that leaves 1. We write this.
- Third, remember that we borrowed an extra bit - so we have 0 - 1 left. We use the same trick to borrow an extra bit, giving us 10 - 1 = 1, which we put in third place.
- Fourth, we again have a leverage for the solution. Subtract the borrowed bit from the existing one: 1 - 1 = 0. We could write this before the result, but since this is the end of the subtraction, there is no need.
Is there a number less than zero ?!
Remember how you learned about negative numbers? Part of the idea is that you can subtract any number from any other number and still get the number. So 7 - 5 is 2; 6 - 5 - 1; 5 to 5 represents 0; What is 4-5? Well, one way to talk about such numbers is to simply apply the same method as above to subtract.
As an example, try 2 - 7 in binary format:
10 - 111 _______ ...1011
I started the same way as before:
- In the far right place, subtract 1 from 0, which requires a borrowed bit. 10 - 1 = 1, so the last bit of the result is 1.
- In the second-rightmost place, we have 1 - 1 with an extra bit of borrowing, so we need to subtract another one. This means that we need to borrow our own bit, giving 11 - 1 - 1 = 1. We write 1 in the second rightmost place.
- Thirdly, there are no more bits in the top number! But we know that we can make the view 0 in front, as we would if the lower number ended a bit. So we have 0 - 1 - 1 due to the battle of borrowing from second place. We need to lend again! In any case, we have 10 - 1 - 1 = 0, which we write in third place on the right.
Now something very interesting has happened - both subtraction operands have no more digits, but we still have a borrowing bit to take care! Well, let me just continue on as we did. We have 0 - 0, since neither the upper nor the lower operand has any bits here, but because of the borrow bit it is actually 0 - 1 ..
(We must borrow again! If we continue to borrow, we will have to declare bankruptcy soon.)
In any case, we take a bit and get 10 - 1 = 1, which we write in fourth place on the right.
Now everyone who has half the mind will see that we will continue to borrow bits until the cow returns home, because there are no more pieces! We ran out of them two places back if you forgot. But if you try to continue, it will look like this:
...00000010 ...00000111 ___________ ...11111011
- In fifth place, we get 0 - 0 - 1, and we borrow a little to get 10 - 0 - 1 = 1.
- In sixth place, we get 0 - 0 - 1, and we borrow a little to get 10 - 0 - 1 = 1.
- In seventh place, we get 0 - 0 - 1, and we borrow a little to get 10 - 0 - 1 = 1.
... And so it goes on for many places as you like. By the way, we just output the binary binary form -5.
You can try this for any pair of numbers that you like, and create two forms of complement to any negative number. If you try to make 0 - 1, you will see why -1 is represented as ...11111111
. You will also understand why all two negative complement numbers have 1 as the most significant bit (the “leading bit” in the original question).
In practice, your computer does not have infinitely many bits to store negative numbers, so it usually stops after some more reasonable number, for example 32. What do we do with the extra bit at position 33? Eh, we just quietly ignore him and hope that no one will notice. When some people notice that our new number system is not working, we call it an integer number of overflows .
Final notes
This is not the only way to make our number system, of course. In the end, if I owe you $ 5, I would not say that your current balance with me was $ 999999995.
But there are some interesting things about the system that we just deduced, for example, the fact that subtraction gives you the correct result in this system, even if you ignore the fact that one of the numbers is negative. Usually we should think about subtractions with conditional steps: to calculate 2 - 7, we first need to find out that 2 is less than 7, so instead we calculate 7 - 2 = 5, and then insert the minus sign in front to get 2 - 7 = -5. But with two additions, we simply continue to do the subtraction and do not care about which number is greater, and the correct result is obtained by itself. And others mentioned that adding works beautifully as well as multiplying.