# Binary Number System

B

A computer is an electronic device. It works on electrical signals. As soon as the power supply to a computer is shut off, it turns off. Any content that is stored on volatile memory is lost when the computer is OFF, and the content stored in non-volatile memory is persistent and can be accessed after again switching on the computer. Thus, a computer can have two states – ON & OFF. Let us call the computer’s ON & OFF states as 1 & 0 respectively. Because of this it can be called a Binary System – i.e. consisting of only two states. A computer can only understand only either 1 or 0 (i.e. ON or OFF), nothing else. And since a computer stores your data in binary, it is always beneficial to understand what the binary number system is. It is all the more important if you are learning programming since every programming language has certain features which will need a basic understanding of binary number system.

Now, if we want to represent 0 in computers, we can simply represent it by the digit 0. If we have to represent the number 1, we can simply represent it as 1. Now, the question arises as to how to represent the numbers 2 and above in the computer’s binary system.

### Decimal System

In our normal day-to-day life, we use the numbers from 0 to 9, i.e. the decimal number system. The numbers 10 and beyond are a combination of numbers from 0 to 9. Similarly, extending this logic to our binary system, the numbers 2 and beyond in the binary system will be respresented as a combination of the digits 0 and 1 only.

Let us understand the decimal number system before going on to understanding the binary number system. The decimal number system has 10 digits (0 to 9). As we go from right to left in a number, the value of each digit increases by a factor of 10. This inherent value is referred to as the Place value of a digit, i.e. the value of the place/position of the digit in the number. The rightmost digit (known as the least significant digit) of a number always has a Place value of 1 (100). As we move left, the value of digits is 10 (101), 100 (102), 1000 (103), and so on in a decimal number system. Thus we see, the Place value of a number increases by a factor of 10 for every digit from right to left. Face value of a digit in a number is the actual digit itself. Therefor the Face Value of a digit will always be a digit from 0 to 9. The actual value a digit in a number is Place Value x Face Value.

Now, let us take the number 23 for understanding how we obtain the value of a number in the decimal number system.

Since the digit 3 is in the rightmost place, its Place value is 1 (100). Its face value is the actual digit, i.e. 3. Therefore the actual value of the digit 3 in the given number is 1 x 3 = 3.
Since the digit 2 is in the second place from right, its Place value is 10 (101). Its face value is the actual digit, i.e. 2. Therefore the actual value of the digit 2 in the given number is 10 x 2 = 20.
Thus, we obtain the value of the given number as 20 + 3 = 23.

Now, let us take the number 723 for understanding how we obtain the value of a number in the decimal number system.

Since the digit 3 is in the rightmost place, its Place value is 1 (100). Its face value is the actual digit, i.e. 3. Therefore the actual value of the digit 3 in the given number is 1 x 3 = 3.
Since the digit 2 is in the second place from right, its Place value is 10 (101). Its face value is the actual digit, i.e. 2. Therefore the actual value of the digit 2 in the given number is 10 x 2 = 20.
Since the digit 7 is in the third place from right, its Place value is 100 (102). Its face value is the actual digit, i.e. 7. Therefore the actual value of the digit 7 in the given number is 100 x 7 = 700.
Thus, we obtain the value of the given number as 700 + 20 + 3 = 723.

### Binary System

Since there are 10 digits in a decimal system, the value of each digit increases by a factor of 10 as we go from right to left in a number. Extending this logic to the binary system, the value of each digit will increase by a factor of 2 as we go from right to left in a number. The rightmost digit (known as the least significant digit) of a number always has a Place value of 1 (20). Similarly the leftmost digit is known as the most significant bit. As we move left, the value of digits is 2 (21), 4 (22), 8 (23), 16 (24) and so on in a binary number system. Thus we see, the Place value of a number increases by a factor of 2 for every digit from right to left. The Face Value of a binary digit will always be either 1 or 0. We have to keep in mind that binary numbers can consist of only 0 and 1. So, let us calculate what the value of 10111 is.

Thus, 23 in decimal is equivalent to 10111 in binary. This can be represented as (23)10 = (10111)2.

Similarly, let us calculate the value of a longer binary number, for example 1011010011.

Thus, 723 in decimal is equivalent to 10111 in binary. This can be represented as (723)10 = (1011010011)2.

Now that we have seen how to convert binary numbers to decimal numbers, let us see how to convert decimal numbers to binary. Let us convert 23 in decimal to binary.

Dividing 23 by 2, we get quotient as 11, and remainder as 1.
Dividing 11 by 2, we get quotient as 5, and remainder as 1.
Dividing 5 by 2, we get quotient as 2, and remainder as 1.
Dividing 2 by 2, we get quotient as 1, and remainder as 0.
Dividing 1 by 2, we get quotient as 0, and remainder as 1.

The division process continues till we get 0 as the quotient.

Now, if we read the remainders from the bottom to up and write them, we get 10111, which is equal to the decimal number 23 as we had already calculated earlier.

As an exercise, you may calculate the binary value of 723, which we is mentioned earlier in this post.

### Binary Arithmetic

Now, let us have a look at binary arithmetic, i.e. the basic mathematical functions of addition and subtraction in binary system.

The basic rule of binary addition is:

• 0 + 0 = 0
• 0 + 1 = 1
• 1 + 0 = 1
• 1 + 1 = 0 (with a carry of 1)

Let us try to add two small binary numbers 101 and 11.

As you see, this is similar to the rules of normal addition which we use with decimal numbers.

#### Subtraction

The basic rule is:

• 0 – 0 = 0
• 0 – 1 = 1 (with a borrow from the left digit)
• 1 – 0 = 1
• 1 – 1 = 0
• 10 – 1 = 1

At times, we have to borrow more than once like we do in decimal arithmetic if there are two or more zeroes together in the number.

Let us try to subtract 101 from 1000

Again, this is similar to the rules of normal subtraction which we use with decimal numbers.

### Binary Logic

Now there are some logical operations that are there in binary system. Logical operations operate on one or two values, either of which can be only 1 (i.e. TRUE) or 0 (i.e. FALSE). The result of a logical operation is different based on the inputs and logical functions.

#### AND

It will give the output as 1 when both the input values are 1.

#### OR

It will give the output as 1 when at least one the inputs values is 1.

#### NOT

AND function simply negates the value of the input.

#### XOR (Exclusive OR)

XOR function gives the output as 1 when only one of the inputs is 1.

#### NAND (NOT AND)

It is the opposite of AND, and gives output as 1 only when both the values are not 1.

#### NOR (NOT OR)

It is the opposite of OR, and gives output as 1 only when both the values are 0. 