Abstract
Decimal | Binary | Hexadecimal |
---|---|---|
Conversion between bases
We convert from one base to decimal base with the help of the radix, then from decimal base convert to the desired base by either division with that particular base for the whole number or multiplication with that particular base for the fraction.
Whole Number Decimal to Binary
- Keep diving by 2 until we get 0
- Then the binary form counts from bottom to top
Important
The idea applies to other bases; replace 2 with the corresponding radix.
Fraction Decimal to Binary
- Keep multiple by 2 until we get 1 or infinite loop
- Then the binary form counts from top to bottom
Important
The idea applies to other bases; replace 2 with the corresponding radix.
Radix
- Also known as Base
- The number of unique digits, including zero, used to represent numbers within that system
- The formula above can be used to convert any base-R to a decimal number
Important
Weights are represented in powers of the radix (R). In base-2, weights are expressed in terms of powers of 2, and in base-10, they are expressed in terms of powers of 10.
Radix representation in C
In C, by default, all numbers are interpreted as base-10 (decimal). To represent numbers in other bases, you can use specific prefixes:
- Prefix
0
for octal (base-8) representation- Prefix
0x
or0X
for hexadecimal (base-16) representation