Binary Math (Adding Ones and Zeroes)
When programming games for a computer, either it being a game console, or a PC, it is very handy to understand how binary numbers work. The Ones (1) and Zeroes (0) that control the computer's brain, the CPU, as well as the bits stored in memory or on physical media.
While bits are patterns of Ones and Zeroes, and can represent anything such a text, or graphics, it is also used to store and calculate numbers. These numbers are stored in binary form, and the CPU knows how to read them. We humans can read them too:
11001011011 is binary for the number 1627. The colors I have chosen is blue for binary, and purple for decimal number (the numbers we humans count with). Reading binary is easy! We have to step though each digit at a time, starting right and move towards left until all bits have been processed. For each step (digit position) we take, we must add a number to our final result. The final result will be in decimal form, and starts at 0 when we begin translating:
The first digit (to the very right) in the binary number is worth exactly 1 decimal. If that digit is 1, then we must add 1 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.
The second digit in the binary number is worth exactly (1 + 1) decimal (which is 2 decimal). If that digit is 1, then we must add 2 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.
The third digit in the binary number is worth exactly (2 + 2) decimal (which is 4 decimal). If that digit is 1, then we must add 4 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.
The fourth digit in the binary number is worth exactly (4 + 4) decimal (which is 8 decimal). If that digit is 1, then we must add 8 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.
Have you notice a pattern? Each digit to the left of the current one's position is worth 2 times as much! That's because binary is base-2. In our decimal system, which is base-10, each digit to the left is worth 10 times that of the current digit's position.
So to read the value 11001011011, we have to go through each digit's position and add how much they are worth, but only if the digit is 1, and not when it is 0! We begin with the final result being 0:
First digit is 1, so the result is 1...
Second digit is 1, so 2 is added to the result making the result total 3 so far...
Third digit is 0, so skip (else you would have added 4 to the result)...
Fourth digit is 1, so 8 is added to the result making the result total 11 so far...
Fifth digit is 1, so 16 is added to the result making the result total 27 so far...
And next digit is 0 so skip (else you would have added 32 to the result)...
And next digit is 1, so add 64 to the result making the total 91 so far...
And next digit is 0, so skip (else + 128)...
And next digit is 0, so skip (else + 256)...
And next digit is 1, so add 512 to the result, total 603...
And the last digit is 1, so add 1024, and the final total result is............................... 1627.
Adding two binary numbers together is done in using the same rules as adding decimal numbers, (by hand) easiest done with the columnar addition approach. Remember that carrying over to the next digit's column is done when an overflow occurs, such as 1 + 1 or (1 carry) + 1 + 1, just similar to how decimal addition works, 9 + 1 or (1 carry, which is worth 10) + 9 + 1. So if you didn't get that last part, it probably doesn't matter as you might be using a different format. The rules of the math is the same though!
Let's do an example. Let's add the number 2 and 1 together, so that the sum is 3. That is easy since you don't need columnar addition... However, in binary, those numbers are 10 and 1 and 11 respectively. So we should/can use columnar addition:
10
+ 1
___
11
First column, 0 and 1 is added to make 1. In the second column, 1 is added to nothing, or 0 if you may, and the final sum is 11.
Let's do another example:
101101 + 10010 _______ 111111
Pretty straight forward. But the numbers here are quite interesting. Because we are using binary instead of decimal, we don't need to use any carry! But the binary number 101101 is the decimal number 45, the binary value 10010 is the decimal number 18, and the binary number 111111 is the decimal number 63, a carry is required when doing this calculation in decimal form:
(1) <- Carry
__
45
+18
___
63
Carrying is done in the same way with binary numbers:
(1) __ 1 + 1 ___ 10
First column, 1 and 1 is added to make 10, but since that number can be stored as a single digit, the digit 1 is carried over to the next column to be added next. In the second column, 1 (the carry) is added to nothing, or 0 if you may, and the final sum is 10.
Of course, there is the case where we must add 1 and 1, as well as a carry (1), and the sum would then be 11. Then 1 is stored in the column, and the other 1 is carried over to the next:
(11) ____ 11 + 11 ____ 110
And that concludes addition of binary numbers. Next up is subtraction, however, it is not as you may think. See you next time!
-
3
41 Comments
Recommended Comments
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Join the herd!Sign in
Already have an account? Sign in here.
Sign In Now