Binary numbers/operations with binary numbers
Tags: binarynumbers, binary
Published: 2022-09-15 21:57:51

**What are binary numbers?**

We use a decimal (base 10) number system in our everyday life. It has 10 values from 0-9, and any other number greater than 9 can be represented by those 10 digits. However, the decimal number system is too complicated for computers to understand. That is why we use a binary number system. In a binary number system, there are only two values, 0 and 1. Any other number can be represented with those two digits. Also, the binary system works well for computers because, in electric cycles, there are also two states when the electricity is on (1) and off (0). 

**How to convert decimal to binary?**

There are 2 ways to convert decimal numbers to binary.
1.	The First method. Let’s convert 67 to binary
If we write 67 as a sum of powers of 2, it’s going to be
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbnx7w9nf81hol9ipb2k.png)

Let’s write down a few numbers that are powers of 2. 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqeuq0c28g2rax99hoiz.png)

The largest number that is a power of 2 equal to or less than 67 is 2^6 = 64. So, to get 67, we will need that 64, so we write 1 in the first place.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acmoujui3ekauap6afi3.png)


To get 67, we also need 2^1 and 2^0, so we write 1s in places of 2^1 and 2^0 and 0s in all other places.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kt29efzfmoujdubc34e9.png)


We got 1000011 which is the binary representation of 67.

2.	The second method is the division method
We need to take a decimal number and divide it by 2. If we get a reminder, we write 1. If we do not get a reminder, we write 0, and so on. Let’s again take 67.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npcj5q5g48trilc3mrfb.png)

And we do this until the end.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qz40byenlip22qynpuj.png)


And we got 1000011 which is 67 represented in the binary number system.

**How to convert binary to decimal?**
To convert a binary number to decimal, let’s write powers of two under the binary digits from right to left. Let’s take 1011, for example.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyn1wius74hcdhh29dxs.png)


Now we need to multiply the 1s and 0s with their matching powers of 2 and add them together, like so
        
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8x6t5qtgmhn3q379gyw.png)


So, 1011 in decimal is 11.

**Addition of binary numbers**
Adding binary numbers is similar to decimal addition, with some differences. Let’s add 1101 and 1001. We need to write them in rows like in traditional addition. We go from right to left and add the first ones using addition rules in the binary system. If we have a carry, we use it in the next column’s addition. Just like that, we can easily add binary numbers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okhm7aaumn16ou8xghha.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cj2egsgzwhyskim8yco9.png)

So, 1101 in decimal is 13, 1001 is 9, 13+9=22, and 10110 is 22, so our addition was correct.

**Subtraction of binary numbers**
To subtract binary numbers, we will also write them in rows. We can subtract binary numbers without a problem using simple binary subtraction rules. Let’s do 110-11.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38qxqtck2zpvojn13er1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nssrg143ba67tvup2x9e.png)

We cannot 0-1, so we borrow a 1 and subtract using this logic. 110 in decimal is 6, and 11 is 3, so our subtraction was correct.

**Multiplication of binary numbers**

Multiplication of binary numbers is pretty simple; we write them in rows, take the second number’s first digit, and multiply it by every digit of the first number. Then we do the same with the other digits of the second number. Then we do the regular addition to get the result of our multiplication.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0c8kef1wful2828v81fq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb9r98aqjpx0bywtomgv.png)


101 in decimal is 5, 11 is 3, and 1111 is 15, so our multiplication is correct.