liveBook · Manning

Bit pattern

As explained in Modern C

A bit pattern is an interpretation of a value as an arrangement of binary digits.

A bit pattern interprets a value as an arrangement of binary digits rather than primarily as a decimal number. For example, print an unsigned value with printf("%x", value) when its individual bits matter. section 8.3.4

Use these distinctions when choosing formatted output:

  • Use "%d" or "%u" for integer values whose numerical meaning matters. section 8.3.4
  • Use "%x" for bit patterns; hexadecimal corresponds more directly to binary digits than octal on architectures with 8-bit character types. section 8.3.4
  • Prefer unsigned values for bit sets, because hexadecimal output receives unsigned arguments. section 8.3.4
  • Match every argument with the correct format specifier and modifier; an inappropriate match produces undefined behavior and may trigger compiler warnings. section 8.3.4
Table 8.8. Hexadecimal values and bit patterns (view table figure)

Digit

Value

Pattern

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
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

Use the hexadecimal-to-binary table to associate each hexadecimal digit with its four-bit pattern. section 8.3.4

Reading hexadecimal digits

The table maps 0 through F to values 0 through 15 and to four-bit patterns: 0 is 0000, 5 is 0101, and F is 1111. Reading hexadecimal output therefore provides a compact view of the corresponding bits. section 8.3.4

How Modern C covers it

How deep it goes
Defines the interpretation and printing choice, then connects hexadecimal digits to four-bit patterns; it stops short of bit manipulation.
Examples
Uses printf("%x", value), hexadecimal digit mappings, and the comparison between hexadecimal and octal output.
What it assumes
Assumes integer values and format specifiers, while defining hexadecimal digits and their corresponding four-bit patterns.
What you can do after
Apply it via printf to display unsigned values as compact hexadecimal representations of their bits.
How it connects
Connects bit patterns with hexadecimal format, unsigned types, bit sets, format specifiers, and undefined behavior.

Also explained in

Compare how 2 books explain bit pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.