DEV Community

Cat4eve
Cat4eve

Posted on

Binary

AUA, Karapet Khachatryan, For Intro to Computer Science course.

In our everyday life, we use numbers, which are taught to us from childhood. However, it is not enough in order to communicate with computers, which were invented in the second half of 1900s. If in the real number system (which can be named as base 10 system, as it uses 10 distinct digits from 0 to 9) we have 10 digits, in binary system (base 2) we have only 0 and 1. The binary is the only way computer undertands our commands, but why? The reason is that computer works by electricity and it has various levels of frequency named Volt. Volt is a difference between potential energy of two sources. It can be imagined as the water flowing from higher potential to lower. Some levels are called 1 and others 0 (for example, 5 Volts = 1, 0.4 Volts = 0), which is named as digital system in engineering (also, there exits analog level, and it's range is distinct, but mostly from 0 to 1023). CPU (Computer's processor) is the main tool used for calculations uses digital system with some level of volts considered as 0 and 1. Here, in binary, we also have some mathematical functions likewise in base 10 system, such that.

0+0 = 0
0+1 = 1
1+0 = 1
1+1 = 10 (here we write 1 usually but in adders we take second digit as a carry, this will be discussed later.)

The equations above are called the truth tables. This table is for adding operations. In math, adding is the same as the union of the sets, or the "or" element in logistics.

0*0 = 0
0*1 = 1
1*0 = 0
1*1 = 1

This one is for multiplication which is the intersection for sets in math and "and" element in logistics.

0^0 = 0
0^1 = 1
1^0 = 1
1^1 = 0

This is xor table and it has no analogs in math but it could be imagined as "a not equal to b is true, then it is false" statement.

The aforementioned tables are in fact functions . To understand what is a function, we can imagine a box where we throw something and box returns us something else like a magic. Those actions are giving input to function and getting output from function. And the box is the function which does something based on input and returns us result.

Before moving on, it is important to mention that with such logic we can recreate base 3, 4, 5, 6, ... systems, where we must add some distinct digit for a new base system. For example, base 16 is 0-9 then A,B,C,D,E,F

The transition between base 2 and base 10 systems is a very important step of understanding binaries. Somewhere, you could have noticed numbers like 10001110, 10101010110 and so on. We already know that they are binaries, but what do they mean? Because of the specifics of various platforms, the same binary could mean different things, so we consider such numbers to be binary representation of base 10 system numbers.

The question is still opened, how to find them. First it's important to understand indexes and their role for the numbers.
13 in base 10 system have 1 and 3 digits, which means it has two indexes, the first one is 3 and second is 1. For base 2 system works exactly the same trick. 001 is binary number, which first index is 1, second is 0 and third is 0 (indexes begins with 1 and increases from right to left). In order to make base 2 to base 10 transition (b2->b10) we use such formula, where we consider n to be the number of indexes and s is the sequence containing all the numbers of binary separately.


s0 * 2^0 + s1 * 2^1 + ... + sn * 2^n

Example: 10000 = 0 * 2^0 + 0 * 2^1 + 0 * 2^2 + 0 * 2^3 + 1 * 2^4

In order to do b10->b2 we must represent the number as the sum of powers of two (for example, 17 = 2^4 + 2^1). This is very similar to the previous one so in order to make binary from base 10 digit we take the highest power of two and write 1 then add from right 0 if the there is no power that would be smaller from highest power by one, else add 1. repeat the process till you get to the power of 0 (example, 17 = 2^4 + 2^0 = 1(4th power of 2) 0(no third power) 0(no second power) 0(no first power) 1 (0 power of 2) => 10010).

Talking about addition operation, I mentioned carry. The carry also exists in general math it is when the memory cell has an overflow (for example in 0-9 space we can't have 10 as it has two digits but not one). In order to make right addition operation we have half and full adders which are represented by "and", "or" and "xor" components.

Half adder gets two binary digits from 0 to 1 as input and returns two digits, one of them is sum, second is carry. For example, 1 and 1 are inputs, we will have 0 as a sum and 1 as a carry. But it is called a half adder because it doesn't condiser upcomming carry from previous operations. For example 11 + 11 = 1+1 then once again 1+1, in the first case we have 0 and 1 as outputs but because of overflow for the next 1+1 we have and additionall carry, which must reult us as 1 and 1 in both outputs. Such solution is called as a full adder and it does exactly the same as pervious one but twice, for this time it also includes a third input, which is a carry from previous operations.

Top comments (0)