DEV Community is a community of 623,427 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Nlogn

Introduction to Binary Number – A number system that your computer understands

mayank joshi Originally published at nlogn.in ・5 min read

What was the first thing that clicked on your head when you saw the number 101? You will say it is one hundred one(if you don’t have any computer science background). But do you know that your computer might interpret it as 5?

Now, let me explain to you what exactly happened here.

Decimal Number System (base-10)

So, the decimals number system consists of those numbers which are used and understood by humans beings. The number is in the range [0 to 9] and any other number is just a combination of multiple digits from this range.

The decimal numbers are base-10 number system.

``````Example: 981 => 9*10ˆ2 + 8*10ˆ1 + 1*10ˆ0
9*100 + 8*10 + 1*1
900 + 80 + 1 = 981 or 98110
``````

Now, you might have understood what decimal number is and what base is, so let’s discuss Binary number i.e, number system understood by your computer.

Binary Number System (base-2)

As I have already mentioned, binary numbers are those numbers which are understood by our computers. They are represented typically using 1’s and 0’s and have base 2. Why?

Our computers are made up of chips and digital circuit, and in the logical world, the circuit can be:

1. Active or Inactive
2. On or Off
3. 1 or 0

Hence the computer only understands 1’s and 0’s, which means anything or everything you see on your computer screen is some weird combination of 1’s or 0’s. So, now let’s talk about how everything is represented using 1’s and 0’s

Ex => 110011, 111100001 are binary number.

Let’s take a binary number say 10101

``````                       (MSB) 1 0 1 0 1 (LSB)
``````

The first bit of binary number is known as Most Significant Bit and the Last Bit of a binary number is called Least Significant Bit.binary

Decimal to Binary Conversion

Let’s you want to represent 35 in a Binary number system:

The important thing to note is that the remainder after division by 2, will always be either 0 or 1.

Binary to Decimal Conversion

Now, let’s convert the number 100011 (base 2) back to the decimal number (base 10).

``````100011 => 1*2ˆ5 + 0*2ˆ4 + 0*2ˆ3 + 0*2ˆ2 + 1*2ˆ1 +1*2ˆ0
=> 1*32 + 0*16 + 0*8 + 0*4 + 1*2 + 1*1
=> 32 + 2 + 1 = 3510 (decimal)
``````

Note, in the binary numbers, we can pad as many zeros as we want after MSB(Most Significant Bit).
Ex: 100011 = 0100011 = 00100011 = 00000000100011 = 35.( but 100011 ≠ 1000110…)

Binary Arithmetics

Binary number supports addition, subtraction, multiplications and division.

Binary addition is the simplest arithmetic operation.

Addition of two single-digit binary numbers:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0, carry 1.

(Decimal 1 + 1= 2 and 1 in binary is represented as 01, so, 01 + 01 = 10 (base-2) and 10 = 1*2 + 0*1 = 2(base-10).

Addition of two multiple-digit binary numbers:

``````      1 1 0 0 0   (carry bit)
0 1 1 0 1  (13 in decimal)
+ 0 1 1 1 0  (14 in decimal)
------------
1 1 0 1 1  (27 in decimal)
``````

2) Subtraction

Binary subtraction is a little tricky.

Let’s understand binary subtraction by first performing single-digit binary subtraction:
0 – 0 = 0
0 – 1 = 1 and borrow 1
1 – 0 = 1
1 – 1 = 0

We can’t subtract 1 from 0, hence we need to take a borrow from the next 1.

``````       0 0 1 0 1 0 1      Borrow
1 1 0 1 1 1 0      (110 in decimal)
−     1 0 1 1 1      (23 in decimal)
----------------
= 1 0 1 0 1 1 1      (87 in decimal)
``````

If that’s a little complicated to understand, let me explain it this way.

The prerequisite for this method is the representation of negative binary numbers and 1’s and 2’s complement of it.

A – B can also be written as A + ¬B +1 (¬B 1’s complement of B and ¬B +1 is 2’x complement of B).

``````Let  A = 1 1 0 1 1 1 0 or 0 1 1 0 1 1 1 0

and  B = 0 0 0 1 0 1 1 1  (we can add as many 0 as much we want at     MSB)

so, ¬B = 1 1 1 0 1 0 0 0   (1's complent of B calulated by flipping
every bit i.e, convert 0 to 1 and 1 to 0)

and, ¬B + 1 = 1 1 1 0 1 0 0 0
+            1
------------------
1 1 1 0 1 0 0 1     (2's complement of B, calculated
by adding 1 to 1's complent of B).

Now, A - B = A + (¬B + 1)
1 1 0 1 0 0 0 0    (carry)
=>  0 1 1 0 1 1 1 0    (A)
+ 1 1 1 0 1 0 0 1    (¬B + 1)
-----------------
0 1 0 1 0 1 1 1    (result)

Now the MSB of the result is 0, which means it's a negative number and
we can use it directly.

A - B = ¬result + 1 = 0 1 0 1 0 1 1 1
``````

Note, if MSB of a binary number is 0, it means it is a +ve number and if the MSB of a binary number is 1, it means it is a negative number and we have to recalculate it 2’s complement to convert it into actual number and multiply it with -1. We will talk more about it in coming posts.

3) Multiplication

The multiplication of two binary numbers is similar to that of decimal multiplication except for the addition part. Let’s understand.

``````                1 0 1 0   (12 in decimal)
x 1 0 1 1   (13 in decimal)
------------
1 0 1 0
+   1 1 0 1 0 |    (green color 1 means carry)
+   0 0 0 0 | |
+ 1 0 1 0 | | |
----------------
1 1 0 1 1 1 0  (12x13 = 156 in decimal)
``````

4) Division

Now, let’s understand the binary division. The binary division is pretty similar to the decimal division except the subtraction part is a little tricky as we have to perform binary subtraction instead of the normal division. Let’s by dividing 57 by 6.

``````    57 = 1 1 1 0 0 1
6 = 1 1 0
and 57/6 = 9 quoitent and 3 reminder

1 0 0 1      (9 quoitent)
______________
1 1 0 ) 1 1 1 0 0 1
− 1 1 0
------
1 0 0 1
−     1 1 0
--------
0 0 1 1  (3 reminder)
``````

Did, we miss something or do you want to add some other key points?🤔