DEV Community

Cover image for BINARY NUMBERS, What are they, how to convert into decimal, operations with them
Elen Galstyan
Elen Galstyan

Posted on

BINARY NUMBERS, What are they, how to convert into decimal, operations with them

In this post I will explain what are Binary Numbers, some other number representations, where they can be used, how to convert binary into decimal and vice versa, how to convert base16 into
decimal and vice versa, how to do operations with binary.

What are Binary Numbers

To begin with, let's talk about what are Binary Numbers. Binary is a numbering scheme that has only two possible values to represent any kind of information. For instance, in decimal we have ten values to represent a number (0-9), however in Binary we have only two digits 1 and 0. Consisting of only two digits makes it much simpler to work with in electronic circuits compared to decimal numbers. This simplicity reduces the chances of errors. Now, you might wonder how to count in Binary. Well, here's how. It is quite easy, as it is just like counting in decimal. You start with 0, then add 1 to count up:
0, 1... Wait! What's next? What will be 1+1 in Binary? It is equal to 10. As we are out of digits to represent 3 (decimal) in Binary, we start using two digits. We don't write 01, as it is the same as 1. So, 0, 1, 10, 11... Again, the same situation. Three digits come to help us, so we get 100. 0, 1, 10, 11, 100, 101, 110 and so on. Now you know how to count in Binary!

What are Binary Numbers used for

What are Binary Numbers used for? I will emphasize a few examples of the usage of Binary Numbers, that I found important to know.

  1. Digital Computers: All data in modern computers is stored and processed in binary. Inside a computer, everything, including text, numbers, images and videos, is represented as a combination of 0s and 1s.
  2. Data Transmission: Binary is used in data transmission and communication protocols. When data is sent over networks or the Internet, it is converted into Binary before transmission and then reconverted at the receiving end. A very good example of this is you listening to music on Spotify or YouTube (think about it).
  3. File Formats: Many file formats used for storing data, such as images, audio and videos are Binary formats. They store data in a structured Binary form to optimize storage and processing.

Other Number Representations

So, we mostly covered what are Binary Numbers, now let's talk about other number representations.

Octal(Base-8): Octal uses eight digits: 0, 1, 2, 3, 4, 5, 6, 7. It's used in some computer programming and permissions systems.

Hexadecimal(Base-16): Hexadecimal uses sixteen digits: 0-9 and A-F, where A represents 10, B represents 11 and so on up to F, which represents 15. It's also used in computer programming for compactly representing large Binary values. In the context of color representation, hexadecimal is often used to represent the values of the red(R), green(G), and blue(B) components in a color.

Base64: Base-64 uses 64 characters, including letters (uppercase and lowercase), numbers and special characters '+' and '/', to represent Binary data. It's commonly used for encoding Binary data into text for safe transmission and storage in various applications.

Congrats! You just learned a lot of information about number representations, now it's time to dive a bit deeper in order to learn conversions.

Converting Decimal into Binary

To convert decimal into Binary, follow these simple steps:

  1. Start with the decimal number you want to convert.
  2. Divide it by 2.
  3. Write down the remainder(either 0 or 1).
  4. Continue dividing the quotient from the previous step by 2 and writing down the remainders until the quotient becomes 0.
  5. Read the remainders from bottom to top. This sequence of remainders represents the binary equivalent of the decimal number.

For example, let's convert the decimal number 13 into Binary. R-remainder
13/2 = 6 (R=1) ^
6/2 = 3 (R=0) ...|
3/2 = 1 (R=1) ...|
1/2 = 0 (R=1) ...|
So, the Binary representation of the decimal 13 is 1101.

Converting Binary into Decimal

To convert Binary into decimal, follow these simple steps:

  1. Start with the Binary Number you want to convert.
  2. Begin from the right and assign each digit a positional value, starting with 2^0, then 2^1, 2^2, 2^3, and so on, incrementing the exponent for each position as you move left.
  3. Continue this process until you've gone through all the digits from right to left.
  4. The sum of the products of all positional values and the corresponding digits is the decimal equivalent of the Binary Number.

For example, let's convert the Binary Number 1101 into decimal.
1 -> 2^3 ^
1 -> 2^2 .|
0 -> 2^1 .|
1 -> 2^0 .|
So, we will get: 2^3*1 + 2^2*1 + 2^1*0 + 2^0*1 = 13
Therefore, the decimal representation of the Binary Number 1101 is 13.

Base16 Conversions

To convert Base16 into decimal and vice versa, you need to use the same logic as for the conversion from Binary to decimal and vice versa, only in this case using the number 16 instead of 2. Try it and you'll see how easy it is.

Conclusion

All right, seems like you now know a lot more about Binary and not only. If you want to learn even more, such as all the operations with Binary, go and watch my YouTube Video, where I explain everything on the examples, so that it's easier to process it at once.
Also, you can check out this article, if you are interested in Python programming language

Top comments (0)