DEV Community

Tigran Baghdasaryan
Tigran Baghdasaryan

Posted on

Guide to Binary Numbers and Other Number Representations

Binary (Base 2)
Digits: 0, 1
Binary numbers are the fundament for computers simply because pc thoughts are a combination of 0 and 1. However, In daily life, we use decimal numbers (Base 10, and the reason why we do that is right in our hands). Converting decimal to binary is so easy. Everything that you need is to divide the required decimal by 2 and collect the remainder(you need to order them starting from the bottom).

For example, the decimal number 162
162/2 = 81 with remainder 0
81/2 = 40 with remainder 1
40/2 = 20 with remainder 0
20/2 = 10 with remainder 0
10/2 = 5 with remainder 0
5/2 = 2 with remainder 1
2/2 = 1 with remainder 0
1/2 = 0 with remainder 1
So 162 in binary will be 10100010.
For doing the opposite operation, take the binary number 10100010, calculate the amount of digits in our case, they are 8 and take out your calculator and lets go(Remember that start is from the and):

(2^0 * 0)+(2^1 * 1)+(2^2 * 0)+(2^3 * 0)+(2^4 * 0)+(2^5 * 1)+(2^6 * 0)+(2^7 * 1) = 162

Octal (Base 8)
Digits used: 0, 1, 2, 3, 4, 5, 6, 7
Well, this type is so rare today it was used in earlier computing systems; therefore, understanding it can be useful when working with legacy code. Anyway, It's enough to remember that each octal digit represents 3 binary digits. Hence, for converting it to base 2 just remember two points.

  1. Convert each octal digit into its 3-digit binary equivalent.

  2. Combine the binary values to get the final binary number.

For example, octal number 157 to binary
First, convert each digit of 157 into binary:
1 in octal = 001 in binary
5 in octal = 101 in binary
7 in octal = 111 in binary

Now, just combine them:
001 + 101 + 111 = 001101111

For the opposite, change 1,5, 7, and 001,101,111 with each other
001 in binary = 1 in octal.
101 in binary = 5 in octal.
111 in binary = 7 in octal.

Now, just combine them together:
1 + 5 + 7 = 157

Converting to decimal(Base 10) from octal(Base 8) is literally the same as in binary (Base 2); Replace 2 with 8 and calculate.
157 from base 8 to base 10 will be
(7*8^0) + (5*8^1) + (1*8^2) = 111

Hexadecimal (Base 16)
Digits used: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Letters in its digits is just continuation of numbers A = 10, B = 11, C = 12, D = 13, E = 14 , F = 15. Hexadecimal is frequently used in computing, especially for representing memory addresses and colors in web design. A single hexadecimal digit represents 4 binary digits (bits).
Converting Base 16 to base 2 is similar to base 8 to base 2. The only difference is that for every base 16 digit, you need 4 base 2.

For example, 2F3 in base 16 to Base 2:
2 = 0010
F = 1111(It's 15 in decimal)
3 = 0011
Combine them, and in binary, we will get 001011110011(also, we can lead zeros if necessary 1011110011).

For converting 2F3 to decimal scheme is the same as for others: (3*16^0)+(15*16^1)+(2*16^2) = 755(in Base 10).

Understanding these conversions is crucial not only in programming, doing your CS homework and computer science but also in various applications like digital electronics, data representation, and even web development.

Top comments (0)