Positional Encoding

Published on November 30, 2024 - Visits: ...

In computer science, there is a fundamental need to represent the entire analog world in digital form. The computer must be able to process images, numbers, text, and other complex types of data. Since the computer is a machine designed to handle complex tasks, its underlying foundation must remain simple.

From a simple foundation, we can conceptually build progressively more complex constructs. At its core, a computer operates entirely with sequences of 0 and 1. Each sequence of bits represents a number in base 2, and thanks to encoding rules—particularly positional encoding—we can transform those numbers into meaningful information.

The Positional Numeral System

The positional numeral system is based on the concept that the value of a digit depends on its position. In the base-10 system, for example, we evaluate position powers of 10.

Base 10 positional numeral system philosophy

For humans, it is natural to think in base 10, largely due to having ten fingers. However, this characteristic does not apply to computers. A computer recognizes only the presence or absence of an electrical signal—on or off (high or low voltage). We use this physical binary state as the foundation to construct a base-2 numerical system.

Conversion from Base-2 to Base-10

Since humans reason in base 10 by convention, there must be a clear way to interpret and convert numbers from base 2 to base 10. We perform this conversion easily by multiplying each bit by 2n based on its positional index and summing the results.

Conversion from Base-10 to Base-2

The reverse requirement also exists: in the physical world, data entry happens in base 10, but we must transmit this information to the computer in base 2 using successive divisions by 2.

Representation of information from base-2 to base-10 Conversion procedure from base-10 to base-2

Base 16 (Hexadecimal)

A convenient middle ground between raw base 2 and human readability is base 16 (Hexadecimal). Base 16 provides a compact representation that is far easier to read and debug than long strings of binary digits.

Representation of information in base 2, base 10, and base 16

As shown above, the same numerical value represented in base 16 is much more compact. Technically, base 16 uses 16 distinct symbols: numbers 0 through 9 and letters A through F (where A=10, B=11, ..., F=15). Each symbol carries a positional weight.

Base 16 offers a structural advantage: each hexadecimal digit represents exactly 4 bits (a nibble).

Hexadecimal digit mapping to 4 binary bits

Base 16 Conversion Methods

Earlier, we saw how the number 45 in base 10 is represented theoretically. One direct algorithm to convert base 10 directly to base 16 involves repeated division by 16:

Direct base-10 to base-16 single algorithm conversion

However, calculating large remainders manually with division by 16 can be error-prone. A more practical approach is to first convert from base 10 to base 2, and then convert base 2 directly to base 16.

First step: Converting base-10 to base-2

Once you have the binary representation, convert to hexadecimal by grouping the binary digits into sets of 4 bits (starting from the least significant bit on the right) and padding with leading zeros if necessary.

Grouping binary bits into 4-bit sets for simple hexadecimal conversion

This two-step grouping method makes converting between decimal and hexadecimal significantly simpler and less prone to manual calculation errors.

← All articles