# Day-14:Binary Number System.

By | May 21, 2022 In a Computer system, computers only understand the binary. The binary system is the representation of numbers in terms of 0’s and 1’s. The Binary Number System is generally used in computer languages like Java and C++ etc. This is due to the fact that the computer only understands the binary language that is 0 or 1. All inputs given to a computer are decoded into a series of 0’s or 1’s.

Number System is generally used for number representation in computer architecture. The Number System is classified into four types.

1. Binary Number System (Base 2)
2. Octal Number System (Base 8)
3. Decimal Number System (Base 10)
4. Hexadecimal Number System (Base 16).

## Number Table.

A decimal number with a few digits can be expressed in binary form using a large
number of digits. Thus the number 66 can be expressed in binary form as 1000010.

The binary form can be expressed more compactly by grouping 3 binary digits together to form an octal number. An octal number with base 8 makes use of the EIGHT digits 0,1,2,3,4,5,6 and 7.

A more compact representation is used by Hexadecimal representation which groups 4 binary digits together. It can make use of 16 digits, but since we have only 10 digits, the remaining 6 digits are made up of the first 6 letters of the alphabet. Thus the hexadecimal base uses 0,1,2,….8,9,A,B,C,D,E,F as digits.

## Decimal Representation(Base-10)

This is because our system is a positional numeral system. Therefore, the value of a given digit depends on its position within the entire number being represented. All the above can be mathematically represented in a very simple way. For example, to represent the value 182736 we can assume that each digit is the product of itself multiplied by 10 powered to its place as an exponent, beginning from the right with 100, following with 101, 102, and so on:

## Octal Representation(Base-8)

Let’s see how we would write the first numbers in octal:

```octal  decimal
-----  -------
0       0   (zero)
01       1   (one)
02       2   (two)
03       3   (three)
04       4   (four)
05       5   (five)
06       6   (six)
07       7   (seven)
010       8   (eight)
011       9   (nine)
012      10   (ten)
013      11   (eleven)
014      12   (twelve)
015      13   (thirteen)
016      14   (fourteen)
017      15   (fifteen)
020      16   (sixteen)
021      17   (seventeen)
```

Thus, for example, the number 17 (seventeen, or XVII in Roman) it is expressed as 021 as an octal number. We can apply the same mechanism that we saw previously for decimal numbers to the octal numbers simply by considering that its base is 8. For example, taking the octal number 071263:

therefore the octal number 071263 is expressed as 29363 in decimal numbers.

Like decimal numbers have 10 different digits to be represented (0123456789) and octal numbers have 8 (01234567), hexadecimal numbers have 16 different digits, that are represented by the numbers from 0 to 9, and the letters A, B, C, D, E and F, which together serve us to represent the 16 different symbols that we need to express base 16 numbers:

```hexadecimal  decimal
-----------  -------
0         0   (zero)
0x1         1   (one)
0x2         2   (two)
0x3         3   (three)
0x4         4   (four)
0x5         5   (five)
0x6         6   (six)
0x7         7   (seven)
0x8         8   (eight)
0x9         9   (nine)
0xA        10   (ten)
0xB        11   (eleven)
0xC        12   (twelve)
0xD        13   (thirteen)
0xE        14   (fourteen)
0xF        15   (fifteen)
0x10        16   (sixteen)
0x11        17   (seventeen)
```

Hexadecimal numbers are preceded by 0x (zero, x).

Once again we can use the same method to translate a number from one base to another one:

## Binary representations(Base-2)

Octal and hexadecimal numbers have a considerable advantage over our decimal numbers in the world of bits and is that their bases (8 and 16) are perfect multiples of 2 (23 and 24, respectively), which allows us to make easier conversions from these bases to binary than from decimal numbers (whose base is 2×5). For example, suppose that we want to translate the following binary sequence to numbers of other bases:

110011111010010100

In order to translate it to decimal, we would need to conduct a mathematical operation similar to the one we have used previously to convert it from hexadecimal or octal, which would give us the decimal number 212628.

Nevertheless, to pass this sequence to octal it will only take us some seconds and even the less skilled in mathematics can do it just by seeing it: Since 8 is 23, we will separate the binary value in groups of 3 numbers:

```110 011 111 010 010 100
```

and now we just have to translate to octal numeral radix each group separately:

```110 011 111 010 010 100
6   3   7   2   2   4
```

giving the number 637224 as result. This same process can be inversely performed to pass from octal to binary.
In order to conduct the operation with hexadecimal numbers we only have to perform the same process but separate the binary value into groups of 4 numbers, because 16 = 24:

```11 0011 1110 1001 0100
3    3    E    9    4
```

Therefore, the binary expression 110011111010010100 can be represented in C++ either as 212628 (decimal), as 0637224 (octal) or as 0x33e94 (hexadecimal).

The hexadecimal code is especially interesting in computer science since nowadays, computers are based on bytes composed of 8 binary bits, and therefore each byte matches with the range that 2 hexadecimal numbers can represent. For that reason, it is so frequently used to represent values translated to or from the binary base.