Binary numbers, their arithmetic
Tags: programming, tutorial, code, web
Published: 2023-09-14 09:47:58

Binary numbers represent one of the number systems computers use. They are comprised of two digits:1 and 0.
Why we need binary numbers?
Binary numbers are an efficient and simple way of transmitting information. As the smallest components of computers are capable of storing 2 values, binary numbers come in handy. Their use is simple because if we compare for example with our lives, basically the simplest way of communicating with another person and as a result getting information is by asking true/false questions. So the numbers 1 and 0 represent values like True and False, and that 2 values are sufficient for transmitting and storing certain data.
How to represent the decimal numbers we are used to in binary?
In our everyday lives we use numbers in decimal system: base 10, which means we can represent each number we have using powers of 10 and integer coefficients. We can try to represent those numbers in decimal system using powers of 2. For the number 17 we write 1x10^1+7x10^0 in base 10, but we can also write 17=1x2^4+1x2^0. What we did now is the first step in representing a number in binary. Now, is we rewrite a bit, 17=1x2^4+0x2^3+0x2^2+0x2^1+1x2^0, and we just take the coefficients we get 10001. Congratulations, we just converted a number from decimal to binary.
We can also convert numbers from binary to decimal. When we are given the binary form like 11001, we should just multiply every digit by 2 to the power of index of the position of that digit. And it is important to remember that the indexing of positions in computer science starts from 0.
So 11001=1x2^4+1x2^3+0x2^2+0x2^1+1x2^0=23.
Besides binary and decimal number systems, there are also other number systems used like octal or hexadecimal. To understand them we have to perceive a certain notion beforehand. When we say a decimal system, and start writing down digits, we notice that after 9 we start using the digits we've already written to form other numbers like 10,11,.... The same happens for binary numbers. As we have only 2 digits, after we write 0 and 1 we start forming other numbers using 1s and 0s. When we try to consider the hexadecimal system(base 16), things get a little more complicated. When we write down the digits from 0 to 9, we notice we still need more digits. We cannot write 10 or 11 because when we write them we already start using the digits we have and get back to decimal system. So we need new digits. As we have used all the digits we know of, we can start using letters. So after 9 we start denoting the numbers by letters A,B,...,F.(A is for 10;B for 11...)
How to add and multiply binary numbers?
The addition and multiplication of binary numbers is somewhat similar to that of decimals. When we add binary numbers, everything is smooth except the case when we are adding 1 and 1. When we have, for example, 110+100, the last two digits of the sum become 10, but when we add 1 and 1, we can't fit the answer in one digit, so we write 0 and carry the 1 to the left. So the final answer becomes 1010. 
In case of multiplication, we conduct it like with decimals, but with slight differences. For example, when we multiply 110 and 100, we multiply each digit of 100 with 110 and write down in separate rows below each other. And with each row, we add 0s in the beginning. Note that if for the second row we have one 0 in the beginning, for the rows below it the number of 0s starts increasing one-by-one(third row-two 0s).
So the multiplication of 110 and 100 is the following:
000+0000+11000=11000.