Imagine if we had only two fingers instead of ten. Would we still use Decimal counting, base-10, zero to nine? Or we would naturally use two digits: zero and one to count: binary numeral system. The following shows some examples of binary numbers (indicated with subscript ) and their Decimal counterparts.
At first sight, the binary system might look confusing. However, the arithmetic operations on the binary number are essentially the same as we have on a Decimal one: we still have carry-over in summation and borrowing in subtraction.
The same applies to multiplication. First, let's try to do simple multiplication on decimal numbers:
Here, we can see that in column 3, we have
and carry over the "extra digit" 2 to the next decimal place.
Let's try the same on binary numbers:
Again, in column 3, we have . What we didn't see in the example of Decimal numbers above is: Here, we need to carry over the additional digits in to the next and the following binary places: to column 5 and to column 4.
In Python, we can represent binary numbers by prefixing
0b to the numbers. So, for the example above, we have:
>>> 0b1111 * 0b111 105
bin() function to convert a Decimal to a binary representation:
>>> bin(105) '0b1101001'