DEV Community

Ani
Ani

Posted on

Binary numbers | AUA | Ani Mikayelyan

This post will cover the topic of binary numbers, conversions between different number representations, and operations with them.
The usual counting system we use in everyday life is the decimal system, also known as base-10 representation, which includes the digits 0, 1, 2, …, 8, 9. It turns out that besides our usual one, there are also other number representations, such as binary (base-2), hexadecimal (base-16), octal (base-8), etc. The binary system contains only two digits, 0 and 1, thanks to which we can send information to the computer. Computers use electronic switches (transistors), which are either on or off. Binary information can represent the state of the switch: 0 is the absence of the signal, and 1 is the presence of it.
All these numeral systems are connected, as there are easy ways to do conversions between them and represent the same number in different bases.
Let’s start by converting a decimal number to binary. We need to divide the decimal number by 2 and write down the remainder, either 1 or 0. Afterward, divide the quotient by 2, write down the remainder, and repeat those steps until the quotient is equal to 0. To get the respective binary number, we need to write the 0s and 1s in the reverse order.
Here is an example:
Represent 450 in binary
450/2=225, remainder: 0
225/2=112, remainder:1
112/2=56, remainder: 0
56/2=28, remainder: 0
28/2=14, remainder: 0
14/2=7, remainder: 0
7/2=3, remainder: 1
3/2=1, remainder: 1
1/2=0, remainder: 1
By writing the remainder in the reverse order we get: 111000010. Therefore, 450(base10) = 111000010(base2).
By this principle, we can convert any decimal number to any other base.
Now, let’s try the opposite, converting binary to decimal. For this one, we need to multiply each digit of a binary number from right to left by the powers of 2 (starting from 0) and get the respective decimal number by adding the results together.
Consider the binary number 111000010, which we got from the previous step; let us check whether we were correct.

Assign values to each digit of a binary number. 111000010 – 2^8, 2^7, 2^6, 2^5, 2^4, 2^3, 2^2, 2^1, 2^0
Now, by multiplying each value by its corresponding binary digit we will get (1 * 2^8) + (1 * 2^7) + (1 * 2^6) + (0 * 2^5) + (0 * 2^4) + (0 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0) which is equal to 256+128+64+0+0+0+0+2+0= 450. So, the binary number 111000010 is, indeed, equivalent to the decimal number 450.
Conversions between other systems are also possible. We may try to convert 111000010 to octal (base 8). By grouping the binary digits into sets of 3, as 2^3=8, we must convert each group into its octal equivalent. Here’s the conversion:
111: 2^2 * 1 + 2^1 * 1 + 2^0 * 0 = 7
000: 2^2 * 0 + 2^1 * 0 + 2^0 * 0 = 0
010: 2^2 * 0 + 2^1 * 1 + 2^0 * 0 = 2
After, write the results together: 702(base 8) = 111000010(base 2)
This was enough with conversions, now; it’s time to move to the operations with binary numbers. Addition and multiplication are very much similar to our ordinary decimal operations.
We got some basic rules for binary addition: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 0 (with carry 1). Having a carry 1 means a shift of 1 into the next column to the left.
An example:
Add 10111 and 00101 together.
10111 + 00101 = 11100
Starting from the right side, 1+1=0 with carry 1, carry1+1+0=0 with carry 1, carry1+1+1=1 with carry 1, carry1+0+0=1, 1+0=1. We get 11100. It is interesting to note that 10111 equals 23 in decimal, 00101 is 5 in decimal, and 11100 is 28 (23+5=28).
Multiplication in binary system.
To multiply two binary numbers, we must multiply each digit of one binary number by each digit of the other and write down each product below each corresponding digit of the multiplier. After, complete the addition described in the previous point.
Here is an example:
Multiply 10111 and 00101.
By multiplying the digits of 00101 by the digits of 10111 me, get the following sum:
10111 + 10111 = 110011 (where 10111 stands 3 digit to the left of 10111)

We have some rules for binary subtraction as well: 0-0=0, 1-0=1, 1-1=0, 0-1=1 with borrow 1.
A simple example:
Subtract 100101 from 111101.
111101 - 100101 = 011000

Negative binary numbers:
One of the methods to represent negative binary numbers is called two’s complement notation. To understand everything faster, let me show the process in the example. Let’s say we want to represent the decimal number -10 in binary. 10 is 1010 in binary. We must flip all the bits (change 0s to 1s and vice versa). The result is 0101; now, add 1 to the new(flipped) binary number: 0101 + 1 = 0110. The binary representation of -10 in two’s complement is 0110.
That was it, thank you!

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Was this an assignment? It seems very odd that a number of posts about binary all appeared on DEV in pretty quick succession.