Binary Math

Binary math is just as simple as decimal math, so is Octal and Hexidecimal. First addition. If we can add then we can subtract. If we subtract then we can multiply and divide. And if we can do those operations we can do all mathimatical operations. In fact, with just 3 mathimatical operations
1) Setting a value to 0
2) Adding 1 to a value
3) Compareing if two values are equal
We can do any and all math operations. (Assumming we also have the ability to recurse and the ability to choose one value out of a list)


Addition:

  Decimal                 Binary

      736                1001010         Here we line up the numbers
     + 44               +  11010         so the rightmost digit are lined.

       1
      736                1001010         Now we start at the right and add
     + 44               +  11010         the digits and move left, we
        0                      0         carry a digit over when needed.

       1                     1           
      736                1001010         Here we carry a number on the base
     + 44               +  11010         two addition.
       80                     00         


      736                1001010         We continue on until we get to the
     + 44               +  11010         end of all the aditions and
      780                1100100         have our answer.
So addition is easy, but what about subtraction?

Lets think about decimal subtration for a minute. Lets say 6 - 2 = 4. Well thats the same as 6 + (-2), so here we add a negitive number to obtain our answer. So now lets figure out how to represent negitive binary numbers.

There were many different forms that we can use for negitive numbers, but if we want to be able to do 6 + (-2) = 4, we need -2 such that....
  0110 ( 6)
 +???? (-2)
  0100 ( 4)
Logic tells us that there is no such number, however, if we limit the size of out binary number to 4 digits 1110 will suffice, since
  0110 ( 6)
 +1110 (-2)
 10100 ( 4)
Then since we only have 4 digits for our answer we throw out the leading 1 and are left with 0100, or 4, which is the correct answer. So we can make a table as follows.

DecimalBinary
0
-1
-2
-3
-4
-5
0000
1111
1110
1101
1100
1011

So what is the pattern to the numbers? Well to make -5 we take 5 in binary, 0101, and reverse all the bits to make 1010 then add 0001 to get 1011. Why do we add 0001? If we didn't then 0000 would be 0 and 1111 would be -0, and that would cause problems. This does limit the values we can represent though, since all values starting with a 1 will be negitive and all values starting with a 0 will be positive.

Examples:
         6   =    0110    =    0110
        -2   =   -0010    =   +1110
         4   =    ????    =   10100

         2   =    0010    =    0010
        -6   =   -0110    =    1010
        -4   =    ????    =    1100
This is called 2s compliment math. There is also 1s compliment, but much like Octal it is not used as often as 2s compliment or Hexidecimal. FYI 1s compliment is when you do not add 0001 after the bit conversion, so -1 = 1110. Math is then done slightly differently in this case but will not be discussed in these notes.

Ok, so theres addition and subtraction. Multiplication and division are just as simple. All you do is write both numbers under each other, then start from the rightmost digit and multiply though.

   Decimal  Binary
      54      110
    x 21     x 11
       4        0
      50       10
      80      100
   +1000       00
    1134      100
           + 1000
            10011
Division is just as simple. Again by example.....

      Decimal          Binary

                                 
   4 / 456      100 / 10101011000


       1                1       
   4 / 456      100 / 10101011000
       4              100
        5               10

       11               10      
   4 / 456      100 / 10101011000
       4              100
        5               101
        4                
        16

       114              101     
   4 / 456      100 / 10101011000
       4              100
        5               101
        4               100
        16                10
        16
         0

                        1010       
                100 / 10101011000
                      100
                        101
                        100
                          101

                        10101      
                100 / 10101011000
                      100
                        101
                        100
                          101
                          100
                            11 

                        101010     
                100 / 10101011000
                      100
                        101
                        100
                          101
                          100
                            110

                        1010101    
                100 / 10101011000
                      100
                        101
                        100
                          101
                          100
                            110
                            100
                             100

                        10101011   
                100 / 10101011000
                      100
                        101
                        100
                          101
                          100
                            110
                            100
                             100
                             100
                                0

                        101010110  
                100 / 10101011000
                      100
                        101
                        100
                          101
                          100
                            110
                            100
                             100
                             100
                                0
Keep in mind that these things are very easy, and very mechanical. more examples will be given in labs as well.