# Binary arithmetic

Jump to navigation
Jump to search

As the name digital implies, digital computers work on on bits with a value of either 0 or 1. Accordingly, all operations, including arithmetic, operate on 0 or 1 values.

Numbers that consists only of 0 or 1s are called **binary** or **base2** numbers.
Binary numbers are often suffixed with a subscript 2, for example 1011011_{2}.

# Converting between decimal and binary

## Decimal as a sum of powers of 10

Any decimal number can be written as a sum of power of 10s. For example:

- 42 = 4*10
^{1}+ 4*10^{0} - 57005 = 5*10
^{4}+ 7*10^{3}+ 0*10^{2}+ 0*10^{1}+ 5*10^{0} - 48.879 = 4*10
^{1}+ 8*10^{0}+ 8*10^{-1}+ 7*10^{-2}+ 9*10^{-3}

Binary numbers can be written in the same way though with powers of 2 instead of 10.

## Converting from binary to decimal

- 1010
_{2}= 1*2^{3}+ 0*2^{2}+ 1*2^{1}+ 0*2^{0}= 8 + 2 = 10 - 100.01
_{2}= 2^{2}+ 2^{-2}= 4 + 1/4 = 4.25

## Converting from decimal to binary

- Divide the decimal number by 2.
- Append the remainder to the
*left*of the binary number. - Repeat with quotient as dividend until it is 0.

For example:

- 29 = 11101
_{2}- 29 / 2 = 14, remainder 1
- 14 / 2 = 7, remainder 0
- 7 / 2 = 3, remainder 1
- 3 / 2 = 1, remainder 1
- 1 / 2 = 0, remainder 1

The same technique can be used for rational numbers.

- First multiply by some power of 2 to convert it to an integer.
- Apply above procedure.
- Divide by the same power of 2 to get the correct result.

# Mathematical operations

The same techniques used for addition, multiplication ... used for decimal numbers can also be used for binary numbers.

## Addition & subtraction

22 10110_{2}+ 19 + 10011_{2}---- -------- 41 101001_{2}

## Multiplication

12 1100_{2}* 5 * 101_{2}---- ------- 10 1100_{2}+ 50 0_{2}---- + 110000_{2}60 --------- 111100_{2}

## Division

372 | 5 101110100 | 101_{2}- 35 +---- - 101 +--------- ---- | 74 ----- | 1001010_{2}22 | 01 | - 20 | - 0 | ---- | ----- | 2 | 11 | - 0 | ----- | 110 | - 101 | ----- | 11 | - 0 | ----- | 110 | - 101 | ----- | 10 | - 0 | ----- | 10 |