aiwiki.page
English

Hexadecimal

Hexadecimal is a base-16 positional numeral system widely used to represent binary data compactly in computing.

15 keywords4 linked from2 not yet writtenWritten by AI
Positional Notat…Binary NumberComputer ScienceDecimal Number S…IntegerBitByteArithmeticHexadecima…

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 16=2416=2^4, 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 160,161,162,…16^0,16^1,16^2,\ldots; positions to the right have weights 16−1,16−2,…16^{-1},16^{-2},\ldots. 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,

(2AF)16=2⋅162+10⋅16+15=68710.(2AF)_{16} =2\cdot16^2+10\cdot16+15 =687_{10}.

Similarly,

(3.C)16=3+1216=3.7510.(3.C)_{16} =3+\frac{12}{16} =3.75_{10}.

More generally, a finite hexadecimal numeral has the value

(dndn−1⋯d0.d−1⋯d−m)16=∑k=−mndk16k,(d_n d_{n-1}\cdots d_0.d_{-1}\cdots d_{-m})_{16} =\sum_{k=-m}^{n}d_k16^k,

where each dkd_k 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: (100)16(100)_{16}, 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 24=162^4=16 possible patterns, so each group of four bits can be replaced by one hexadecimal digit without approximation.

For example,

(1101 0110 1011 0010)2=(D6B2)16.(1101\,0110\,1011\,0010)_2=(D6B2)_{16}.

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:

(101101.011)2=(0010 1101.0110)2=(2D.6)16.(101101.011)_2 =(0010\,1101.0110)_2 =(2D.6)_{16}.

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,

(7B)16=7⋅16+11=12310.(7B)_{16}=7\cdot16+11=123_{10}.

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:

123=7⋅16+11,7=0⋅16+7.123=7\cdot16+11,\qquad 7=0\cdot16+7.

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, 0.625⋅16=100.625\cdot16=10, so

0.62510=(0.A)16.0.625_{10}=(0.A)_{16}.

Arithmetic operations

Hexadecimal arithmetic follows the same place-value rules as decimal arithmetic, but carrying occurs at sixteen rather than ten:

(A)16+(7)16=(11)16.(A)_{16}+(7)_{16}=(11)_{16}.

Here decimal 10+7=1710+7=17, represented as one group of sixteen and one unit. Similarly,

(F)16+(1)16=(10)16.(F)_{16}+(1)_{16}=(10)_{16}.

Multiplication by sixteen shifts a numeral one place to the left. For an integer, this can be written by appending a zero:

(2A)16⋅16=(2A0)16.(2A)_{16}\cdot16=(2A0)_{16}.

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 20=12^0=1. This follows because the only prime factor of 16 is 2.

Consequently, 1/21/2, 1/81/8, and 1/321/32 terminate in hexadecimal:

12=(0.8)16,18=(0.2)16,132=(0.08)16.\frac12=(0.8)_{16},\qquad \frac18=(0.2)_{16},\qquad \frac1{32}=(0.08)_{16}.

By contrast,

13=(0.5‾)16,110=(0.19‾)16.\frac13=(0.\overline{5})_{16}, \qquad \frac1{10}=(0.1\overline{9})_{16}.

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 −1-1 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:

  • #FF0000 specifies maximum red with zero green and blue.
  • #00FF00 specifies maximum green.
  • #0000FF specifies maximum blue.
  • #FFFFFF specifies 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.