Hexadecimal is a positional numeral system with base 16. It uses sixteen digit symbols, conventionally 0–9 for values zero through nine and A–F for values ten through fifteen. Lowercase a–f is also common. Because , each hexadecimal digit corresponds exactly to four binary digits, making hexadecimal a compact way to write binary numbers. It is widely used in computer science for displaying memory contents, machine instructions, addresses, colors, and other digitally represented data.
Digits and place value
In hexadecimal, successive positions to the left of the radix point have weights ; positions to the right have weights . The radix point serves the same purpose as the decimal point in the decimal numeral system.
| Decimal value | Hexadecimal digit | Four-bit binary representation |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| 10 | A | 1010 |
| 11 | B | 1011 |
| 12 | C | 1100 |
| 13 | D | 1101 |
| 14 | E | 1110 |
| 15 | F | 1111 |
For example,
Similarly,
More generally, a finite hexadecimal numeral has the value
where each is an integer from 0 to 15. The letters A–F are digit symbols here, not algebraic variables.
Leading zeros do not change the numerical value: 002A and 2A both denote decimal 42. In computing, however, leading zeros can indicate a fixed field width or preserve the layout of stored data.
Notation conventions
A string such as 100 is ambiguous unless its base is established: hexadecimal 100 represents decimal 256, not decimal 100. Common ways to identify hexadecimal include:
- A subscript: , especially in mathematics.
- The prefix
0x:0x100, used in C and many other programming languages. - A suffix
h:100h, used in some assembly-language conventions. - A dollar-sign prefix: used in some assemblers and programming environments.
- A hash-sign prefix: used for hexadecimal color notation in web technologies.
These conventions are context-dependent. A hash sign, for example, does not generally mean that any following text is a hexadecimal number.
Where both uppercase and lowercase digits are accepted, 0xBEEF and 0xbeef have the same numerical value. Some assembly syntaxes require an initial decimal digit when using an h suffix, so a value beginning with a letter may be written 0FFh.
Relationship to binary
The principal practical advantage of hexadecimal is its exact alignment with binary. Four bits have possible patterns, so each group of four bits can be replaced by one hexadecimal digit without approximation.
For example,
Conversion in the reverse direction consists of replacing each hexadecimal digit with its four-bit pattern. Unlike conversion between binary and decimal, no division or multiplication is necessary.
An eight-bit byte is represented by exactly two hexadecimal digits, from 00 to FF, corresponding to decimal values 0 through 255. Likewise, a 32-bit pattern takes eight hexadecimal digits, while a 64-bit pattern takes sixteen.
For a binary number with a fractional part, groups are formed outward from the radix point. Zeros may be added at the far left of the integer part or the far right of the fractional part:
This correspondence makes hexadecimal useful for inspecting individual bits, flags, and fixed-width fields.
Conversion and arithmetic
Conversion to and from decimal
To convert hexadecimal to decimal, multiply each digit by its positional weight and add the results. For example,
To convert a nonnegative decimal integer to hexadecimal, repeatedly divide by 16 and record the remainders. Reading the remainders in reverse order gives the hexadecimal digits. For decimal 123:
The remainders are 11 and 7, giving 7B.
A fractional part can be converted by repeatedly multiplying it by 16. At each step, the integer part of the product supplies the next hexadecimal digit. For example, , so
Arithmetic operations
Hexadecimal arithmetic follows the same place-value rules as decimal arithmetic, but carrying occurs at sixteen rather than ten:
Here decimal , represented as one group of sixteen and one unit. Similarly,
Multiplication by sixteen shifts a numeral one place to the left. For an integer, this can be written by appending a zero:
Changing the numeral system changes the representation, not the underlying number or the rules of arithmetic.
Terminating and repeating fractions
A rational number has a terminating hexadecimal expansion precisely when its denominator, after reduction to lowest terms, is a power of two, including . This follows because the only prime factor of 16 is 2.
Consequently, , , and terminate in hexadecimal:
By contrast,
Thus a fraction that terminates in decimal need not terminate in hexadecimal. These properties follow from the denominator’s prime factors, rather than from the choice of digit symbols.
Uses in computing
Memory, machine code, and debugging
Hexadecimal is commonly used to display memory addresses, instruction encodings, and raw data. A hexadecimal dump typically shows successive bytes as pairs of hexadecimal digits, sometimes alongside a character interpretation.
Hexadecimal notation does not determine the meaning of those bytes. The pattern FF might represent the unsigned integer 255, the signed integer in an eight-bit two’s-complement representation, part of a machine instruction, or some other encoded data.
Likewise, endianness determines the order of bytes in a multi-byte value, not the order of hexadecimal digits within a displayed byte. The value 0x12345678 is stored as bytes 12 34 56 78 in big-endian order and 78 56 34 12 in little-endian order.
Bit masks
Hexadecimal digits correspond to four-bit groups, making them convenient for masks used in bitwise operations. For example, the mask 0x0F selects the lowest four bits of a byte when used with bitwise AND. The mask 0x80 identifies its highest-order bit.
This is a representation-level application of Boolean algebra: the hexadecimal notation makes the bit patterns easier to read but does not change the logical operation.
Colors
In web styling, hexadecimal notation commonly specifies colors in the RGB color model. A six-digit form, #RRGGBB, assigns two hexadecimal digits to each red, green, and blue component:
#FF0000specifies maximum red with zero green and blue.#00FF00specifies maximum green.#0000FFspecifies maximum blue.#FFFFFFspecifies maximum values for all three components.
An eight-digit form, #RRGGBBAA, adds an alpha component for opacity. Short forms such as #RGB expand by duplicating each digit; for example, #3A7 expands to #33AA77.
Character identifiers and encoded data
Unicode code points are conventionally written using U+ followed by hexadecimal digits. For example, U+0041 identifies the Latin capital letter A. A code point is an abstract identifier; it is not necessarily identical to the bytes used to encode that character.
Hexadecimal also appears in cryptographic digest displays, network diagnostic output, and percent-encoded byte values. Representing data in hexadecimal is not encryption: it is a reversible change of notation that provides no secrecy.
Historical development and limitations
Hexadecimal became prominent with the growth of binary digital computing in the mid-twentieth century. Systems organized around eight-bit bytes made its two-digits-per-byte correspondence particularly useful. IBM’s System/360 family, introduced in 1964, was an influential platform using hexadecimal notation in a byte-oriented architecture.
Other bases have also been important in computing. Octal, or base eight, maps each digit to three bits and is convenient for binary fields whose widths are multiples of three. Hexadecimal is especially convenient when widths are multiples of four.
Hexadecimal is compact compared with binary, but it is generally less familiar than decimal for everyday quantities. Unmarked strings can be ambiguous, and a hexadecimal display alone does not establish signedness, field width, byte order, character encoding, or data type.
Hexadecimal notation should also be distinguished from floating-point arithmetic. A binary floating-point value can be displayed using hexadecimal digits without becoming a base-16 floating-point format. Separately, some floating-point architectures use 16 as their arithmetic radix. The notation used to print a value and the format used to store or calculate it are distinct choices.