Number System?! Yup! Get to know your numbers well, engineers.
'Week 1' at cs.code.in - engineering redefined.
The following piece covers the basics of 'Computer Number System.' So, dive in to exercise your intellect🧠
Let's learn to count... again.
We are acutely familiar with the symbols: 0, 1, 2, …, 9, which form the so-called decimal number system (DEC). When we count, all we do is put those symbols in one-to-one correspondence with quantities like one, two, and so on. But, in DEC, there are no symbols for quantities > nine. So we go back to 0, and put a 1 to its left, forming 10 (ten). Then, we move ahead as: 11, 12, 13… After nineteen, we are again out of symbols, where do we go from here? Well, we use a similar solution, go back to 0, but this time put 2 to its left — 20 (twenty).
Proceeding like so, there's no issue till ninety-nine (99) at which point we’d have exhausted all the possible — ignoring the ones with 0 on LHS — two-digit arrangements. For higher quantities, we play a similar game: move back to 0, and add one to the left-most digit of 99, generating 100 (hundred).
The binary system (BIN) only has 0 and 1. It’s similar to DEC, it’s just that we reach “ten” pretty quickly here — at two, we are out of symbols. So back to the good old 0, and put 1 to its left. Thus, 2 in decimal is equivalent to 10 (one-zero) in binary. Mathematically,
$$(2)_{10} \equiv (10)_2$$
The following table compares DEC with BIN.
** Smartphone users, plz go landscape for better viewing 🙃
Decimal no. | Binary no. |
0 | 0 |
1 | 1 |
2 | 10 |
3 | 11 (out of two-digit permutations) |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 (even out of three-digit ones) |
8 | 1000 |
9 | 1001 |
Positions matter!
Any digit of a decimal number has a place value that is solely dependent on its position. Each such value is a power of 10, e.g., for a 3-digit number, starting from the right, the places are labeled as: ones (10^0), tens (10^1), and hundreds (10^2).
Likewise, BIN is also a positional system, only the powers of 10 are replaced by powers of 2. Given a base-2 number, we can express it in terms of powers of 2, e.g.,
$$1011 \rightarrow 2^3 + 2^1 + 2^0 = (11)_{10}$$
** The place value is a power, which is one less than the digit's position-index.
A couple more systems
Octal system (OCT), as the name suggests, has eight digits: 0, 1, 2... 7. From the above reasoning, a base-8 number is associated with powers of 8. Base-8 is commonly used as a shorthand for binary numbers — bits triplets (more on this later).
Hexadecimal system (HEX) has sixteen digits: 0, 1... 9, a, b, c, d, e, f. Here, powers of 16 come into play.
System conversions
DEC —> BIN
Given a base-10, say 153. To find the equivalent base-2, divide 153 by 2, and note the quotient (Q) and the remainder (R). Then, do Q/2, and record the new Q and R. Continue the process until the quotient becomes 0. The required BIN is the sequence of bits in the R-column (bottom-up).
$$(153)_{10} \longrightarrow (10011001)_2$$
Q | R |
76 | 1 |
38 | 0 |
19 | 0 |
9 | 1 |
4 | 1 |
2 | 0 |
1 | 0 |
** For base-8 and base-16, just replace the divisor by 8 and 16 respectively.
OCT, HEX —> BIN
Well, the general way is to convert the oct or hex into dec, and then use the above method for bin. But there's a neat trick to it.
💡Base-8: replace each digit with its equivalent triplets of bits.
$$(231)_8 \rightarrow (10\ 011\ 001)_2$$
Digits | Triplets |
2 | 010 |
3 | 011 |
1 | 001 |
💡Base-16: replace each digit with its equivalent group of 4 bits. Consider ABC7.
$$\text{Binary: } (1010\ 1011\ 1100\ 0111)_2$$
Digits | Equiv 4 bits |
A | 1010 |
B | 1011 |
C | 1100 |
7 | 0111 |
float DEC —> BIN
Consider a regular decimal, 10.16. To get the binary, we start from the left (10), and find its binary as usual. So,
$$(10)_{10} = (1010)_2$$
Let's move to the mantissa (.16). Multiply it by 2. Take the decimal part of the product, and multiply by 2 again. The integral parts of the products form the mantissa for the BIN. Repeat until the required no. of decimal places is achieved.
Mantissa | x 2 |
.16 | 0.32 |
.32 | 0.64 |
.64 | 1.28 |
.28 | 0.56 |
$$\therefore\ (10.16)_{10} = (1010.0010)_2$$
** The above binary is truncated to 4 decimal places.
** For any other base, say OCT, just multiply by 8 instead of 2.
Thanks😊
*To be continued...*✌️
Subscribe to my newsletter
Read articles from Atanu Sarkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Atanu Sarkar
Atanu Sarkar
I am a computer science engineering student at The Hacking School - Coding Bootcamp. 💻