The numbers we use everyday are known decimal numbers or base 10. Meaning there are 10 numbers ranging from 0 to 9. This translate to 103 102 101 100 or 1000 100 10 1. So to write number 205, will be same as (1000x0)+(100x2)+(10x0)+(1x5) = 205.Or you can get the same result by division. Remainder is written on the right. Results read from bottom up. I know you know all this, but it just to refresh your memory.
Now for the fun stuff.
In base 2 (BINARY), only 2 numbers are available for use. They are 0 and 1.
27 26 25 24 23 22 21 20. Which translates to 128 64 32 16 8 4 2 1. So to represent 20510 we will need (128x1)+(64x1)+(32x0)+(16x0)+(8x1)+(4x1)+(2x0)+(1x1). Which translate to 11001101. There is an easier method of doing this, division by 2.
So now eight base 2 numbers are used, that is to say 8 bits (BInary digiTS).
Remainder is written on the right. The answer is read from bottom up. The number is written as 110011012 and in MikroC is written as 0b11001101. All binary numbers are represented by 0b prefix. Simple isn’t it? For results with less than 8 bits, zeros are placed on the left of the answer until the bits count is 8. ie. result is 1100. Then result 00001100. Also good to know is that the leftmost 4 bits are known as high order bits and the rightmost 4 bits are known as low order bits.
Some useful info.
4 bits = 1 nibble
8 bits = 1 byte
16 bits = 1 word
Base 8 is known as OCTAL. The process of converting base 10 to base 8 is similar to the above example except you divide with 8. So lets convert 20510 to base 8.
The answer is 3158 and octal numbers are represented in MikroE C by preceding the number with 0 (zero), which will be written as 0315. This number can be reversed to base 10 by 82 81 80 which is 64 8 1. So (64 x 3) + (8 x 1) + (1 x 5) = 205.
In base 16, known as HEX, numbers 0 to 9 and letters A to F are used to represent the number. Because we cannot use a double digit number like 11,12 and so forth, the letters A to F is used to represent a double digit number, A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15. Let’s convert 20510 to hex by dividing.
As you can see, we cannot write 12 and 13. So we change the number to their alphabet equivalent. So the answer will be BC16. Again we can verify if the answer is correct by using the 161 160. Which is 16 and 1. We know that B is 12 and C is 13, so (16 x 12) + (1 x 13) which gives us a decimal 205. Hex number is represented as 0xnn in MikroC. Our number will be written as 0xBC. The 0x tells the compiler that the next digits are hex number. If the division yields only one number, than a 0 precedes the number. ie. 0x0B.
That’s all there is to understanding base n. To convert a number from base 8 to base 2, firstly the number needs to be converted to base 10 then to base 2. There no calculation method to directly convert the number. I will present you later with the table to do exactly that.
Lets do some exercise so that base n sticks in our minds.
Skill check Lesson 5.
1. Convert to binary.
a. 1010 b. 25510 c. 10110 d. 6810
2. Convert to decimal.
a. 101011002 b. 100100112 c. 011011112 d. 10012
3. Convert to octal.
a. 25210 b. 9710 c. 1810 d. 12710
4. Convert to hex.
a. 25510 b. 12610 c. 16010 d. 3210
You can post your answers in the comments. Chat box might get confusing.