Binary representations

Binary representationsOctal and hexadecimal numbers have a considerable advantage over our decimal numbers in the world of bits, and is that their bases (8 and 16) are perfect multiples of 2 (23 and 24, respectively), which allows us to make easier conversions from these bases to binary than from decimal numbers (whose base is 2x5). For example, suppose that we want to translate the following binary sequence to numbers of other bases:

110011111010010100

In order to translate it to decimal we would need to conduct a mathematical operation similar to the one we have used previously to convert from hexadecimal or octal, which would give us the decimal number 212628.

Nevertheless to pass this sequence to octal it will only take us some seconds and even the less skilled in mathematics can do it just by seeing it: Since 8 is 23, we will separate the binary value in groups of 3 numbers:

110 011 111 010 010 100

and now we just have to translate to octal numberal radix each group separately:

110 011 111 010 010 100 6 3 7 2 2 4

giving the number 637224 as result. This same process can be inversely performed to pass from octal to binary. In order to conduct the operation with hexadecimal numbers we only have to perform the same process but separating the binary value in groups of 4 numbers, because 16 = 24:

11 0011 1110 1001 01003 3 E 9 4

Therefore, the binary expression 110011111010010100 can be represented in C++ either as 212628 (decimal), as 0637224 (octal) or as 0x33e94 (hexadecimal).

The hexadecimal code is specially interesting in computer science since nowadays, computers are based on bytes composed of 8 binary bits and therefore each byte matches with the range that 2 hexadecimal numbers can represent. For that reason it is so frequently used to represent values translated to or from binary base.