# Binary Number System : Counting With Ones And Zeros

## Introduction to Number Systems Number systems in mathematics are devised to keep track of the several aspects of computing, and calculation including arithmetic – basics of addition, subtraction, multiplication and division – among other conceptual issues. Everyday, we balance our accounts, pay bills, and settle grocery lists using the well known decimal system initially taught as a part of grade school education. Computer arithmetic is however carried out in the binary system. For example, programming languages are used to instruct the computers in these binary languages.

To learn from this article, it is best to make a distinction between numbers as a concept and its written forms as number system. For example, the number one hundred is written as ‘C’ in Roman numeral system, 100 in decimal system we use daily, and 64 in hexadecimal (base-16) system – and regardless of the system of representation, we have the same properties of hundred – it is even, composite, power of 10 raise to two, etc. The ancients knew something about counting with fingers, because words and digits have its etymology tracing back to the fingers and toes.

## Binary System Compared to Decimal System

The decimal system is a method of computing and natural system of counting where we have 10-digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 to represent numbers. Rules of decimal arithmetic are easy and we know these in practice,

1. Addition rules are, 0 + 1 → 1, 2+3 → 5, 9 + 0 → 9, 9 + 8 → 17 etc

2. Subtraction rules are, 0 – 1 → -1, 5 – 9 → -4, etc.

3. Multiplication rules are, 0*1 → 0, 1*1 → 1, 8*5 → 40, etc.

4. Division rules are defined such, when 40/5 → 8 | 40 = 5*8, etc.

Fractional numbers in the decimal system are written to the right of a decimal point. Each position to the left of decimal point is valued at 10 raised to a negative power.

To write the number five-tenths plus 7 hundredths we will use a notation like, 5/10 + 7/100 = 0.57 = 5*10^-1 + 7*10^-2.

Note: This article uses the computer programming notation for exponent or power operation ‘^’ to write, 1/10 = 10^-1, and 1/100 = 1/(10*10) = 1/(10^2) = 10^-2.

## Elementary Operations in Binary Arithmetic

In this section we will see how you can carry out elementary binary operations like, addition, subtraction, multiplication and division.

1. Addition rule are: 1 + 1 → 10, 0 + 0 → 0, 0 + 1 → 1, 1 + 0 → 0

2. Multiplication rules are: 1 x 1 → 1, 1 x 0 → 0, 0 x 1 → 0, 0 x 0 → 0

3. One’s complement of binary digit: ~1 =0 , ~0 = 1. Complement can be thought of as logical negation at digit level. Ones complement of a number is doing the ones complement at every digit. i.e. 1’s complement of (10110)2 → (01001)2

4. Logical operations on binary digits:

1. AND: 0 AND 0 → 0, 0 AND 1 → 0, 1 AND 0 -> 0, 1 AND 1 → 1

2. OR: 0 OR 0 → 0, 0 OR 1 → 1, 1 OR 0 -> 1, 1 OR 1 → 1

3. XOR: 0 XOR 0 → 0, 0 XOR 1 → 1, 1 XOR 0 -> 0, 1 XOR 1 → 1

5. Subtraction rules are same as addition except the second operand is converted to two’s complement notation before addition. For example,

6. Division is carried out using fractional binary numbers, including a decimal point.

## Some conventions

Two’s complement of a number, usually used in binary subtraction, is defined as the taking the one’s complement (or just complement we talked about earlier in 3). Twos complement of(10110)2 is got by taking the 1’s complement (10110)2 → (01001)2 and adding 1 →. (01010)2.

The leading bit position in the number (left-most) (10110) is called most significant bit (MSB), is ‘1’, and the (right-most) bit is called least-significant bit (LSB), ‘0’ in this case.

## Example – Converting Binary to Decimal in Python

In this example we will write a Python program to convert a binary number to decimal. We will also handle the fractional binary numbers.

1. Initialize return value to 0
2. First start at the MSB, and upto the decimal point, and repeat steps 3-4
3. Find the power of two at the position, if the bit-position is 1, or use 0.
4. Add return value to the previous result of step-3.
5. Repeat 3-4 till you reach decimal point.
6. For each bit position upto end of number repeat the steps 7-8
7. If bit position is 0, continue to next right-most, otherwise add the negative power of 2 raised to offset from the decimal point
8. Add return value to this number calculated in the 7.
9. Repeat 7-8 till end of the number.

To illustrate manually, this algorithm works out as follows,

(101.101)2 = 1 x 2^2 + 0x2^1 + 1×2^0 + 1×2^-1 + 0x2^-2 + 0x2^-3

= 1 x 4 + 0 x 2 + 1 x 1 + 1 x ½ + 0x ¼ + 1 x 1/8

= 5 + ½ + 1/8

= 5.625

Putting it all together we get the Python code, and saving it as ‘bin2dec.py’

```     1	def bin2dec( binstr ):

2	    dec_pos = binstr.find('.')

3	    assert( dec_pos != -1 )

4

5	    val = 0.0

6	    for itr in range(0,dec_pos):

7	        pow2 = int(binstr[itr])*2**(dec_pos-itr-1)

8	        val = val + pow2

9

10	    for itr in range(dec_pos+1,len(binstr)):

11	        neg_pow2 = int(binstr[itr])*2**(dec_pos-itr)

12	        val = val + neg_pow2

13	    return val

14	if __name__ == "__main__":

15	   print bin2dec('1011.11')

16	   print bin2dec('101.101')
```

which you can run on the Python shell, and you will see the following results, when you input

```\$ python bin2dec.py
11.75
5.625
```

## Summary

Number systems in mathematics are devised to keep track of the several aspects of computing, and calculation including arithmetic – basics of addition, subtraction, multiplication and division – among other conceptual issues. Everyday we balance our accounts, pay bills, and settle grocery lists using the well known decimal system, initially taught as a part of grade school education. Computer arithmetic is however carried out in the binary system. These are foundations for large and complex programs like web crawlers, machine learning software, and other intelligent systems.