DEV Community

Narek Jumayan
Narek Jumayan

Posted on

A Beginner’s Guide to Binary Numbers and Operations

In this post I will explain what binary numbers are, and show you how to do some basic operations in binary. We typically use the decimal number system in our daily lives, from counting money, to telling time, most likely it's due to the count of our fingers. Every digit in the decimal system represents a distinct power of ten. For example, 365 = 3×10^2+5×10^1+6×10^0. However, computers are unable to use the decimal system instead, they can use binary numbers, sometimes called base-2 numbers. Millions, if not billions, of tiny switches known as transistors make up computers. There are only two possible states for these transistors: on and off. Assigning a 1 to "on" and a 0 to "off" Binary numbers, which are made up of only 0s and 1s, fit in nicely with transistors, which can only represent two states.
Just like the decimal system, the binary system represents numbers with powers. The main difference is that binary numbers use powers of 2 rather than powers of 10. In binary 0 is 0 and 1 is 1, but how do you represent 2? In binary numbers , each digit represents a power of 2.Just like in the decimal system, when you run out of digits you add a number to the left. This occurs after 1 in binary, so if you add 1 to the left, you get 10, which in binary will represent 2.
Here are a few numbers represented in binary.

  • Binary 10 is the same as decimal 2.
  • Binary 11 is the same as decimal 3.
  • Binary 100 is the same as decimal 4.
  • Binary 101 is the same as decimal 5.
  • Binary 110 is the same as decimal 6.
  • Binary 111 is the same as decimal 7.
  • Binary 1000 is the same as decimal 8.

Notice that each time you add a digit from left to binary numbers, you double the range of numbers it can represent similarly to the decimal system where adding a digit from left will increase the numbers it can represent by a power of 10.
To convert a binary number to decimal you have to take each digit starting from right and multiply it with corresponding power of 2 starting with 0 for the rightmost one. For example,1101 = 1×2^0+0×2^1+1×2^1+1* 2^3 = 8+4+0+1=13. So 1101 in binary is 13 in decimal.
Adding binary numbers is done similarly to decimal addition, but you carry over a number when the sum reaches 2 not 10(since we are working in base-2).Let's add two binary numbers: 101 and 011.

Image description

Step-by-step:
Rightmost column: 1 + 1 = 2 (write 0, carry 1)
Next column: 0 + 1 + 1 (carry) = 2 (write 0, carry 1)
Next column: 1 + 0 + 1 (carry) = 2 (write 0, carry 1)
Final carry: 1 (write it down)
So, 101 + 011 = 1000.
Subtraction in binary also has a concept of "borrowing," like in decimal. The key difference is that when you borrow in binary, you're borrowing 2 instead of 10, to make it easier you can add two 1s to the column you borrow for(Since 10 = 1 + 1 in binary system).
Let’s subtract 110 (6 in decimal) from 1011 (11 in decimal):

Image description

Step-by-step:
Start from the right: 1 - 0 = 1
Move left: 1 - 1 = 0
Next: 0 - 1 (we can’t do this, so we borrow 1 from the next column). This turns the 0 into 2 (in binary, borrowing turns a bit into 2,we can represent it as 1 + 1), and now: 1 + 1 - 1 = 1
Finally, we have 0 (since we borrowed earlier).
Binary multiplication is quite simple and works like long multiplication in decimal, but instead of multiplying by digits 0-9, you only multiply by 0 or 1. Here’s an example of multiplying 101 (5 in decimal) by 11 (3 in decimal):

Image description

So, 101 × 11 = 1111 (which equals 15 in decimal).

To understand better how computer works with binary numbers I recommend you to watch my YouTube video where I explain logic gates and how they are used to add and subtract numbers: https://youtu.be/IhlNuZtn5Qo

Top comments (0)