Number System?! Yup! Get to know your numbers well, engineers.

Binary and more.

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.
00
11
210
311 (out of two-digit permutations)
4100
5101
6110
7111 (even out of three-digit ones)
81000
91001

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$$

QR
761
380
190
91
41
20
10

** 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$$

DigitsTriplets
2010
3011
1001

💡Base-16: replace each digit with its equivalent group of 4 bits. Consider ABC7.

$$\text{Binary: } (1010\ 1011\ 1100\ 0111)_2$$

DigitsEquiv 4 bits
A1010
B1011
C1100
70111

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.

Mantissax 2
.160.32
.320.64
.641.28
.280.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...*✌️