liveBook · Manning

Stdc_bit_width

As explained in Modern C, Third Edition

stdc_bit_width is a C23 facility that identifies the highest-positioned one-valued bit through the width of an integer's significant bit representation.

stdc_bit_width is a C23 integer-arithmetic facility from <stdbit.h>. It returns the width of an integer's significant bit representation: 1 + floor(log2(x)), or 0 when x is zero. section 8.2 section 5.7.2

Reading the highest set bit

For x = 8, the significant representation is 1000, so stdc_bit_width(x) is 4. The result describes the position of the highest-valued one bit through the representation's width, rather than depending on the declared type's total width. section 8.2 section 5.7.2

TAKEAWAY

  • Use stdc_bit_width when exceptional argument cases need handling; its defined results cover all argument values. section 8.2
  • Prefer the type-generic interface when available, because the result is independent of the argument's type. section 8.2
  • Check platform support before relying on <stdbit.h>; C23 support may not yet be available. section 5.7.2
Table 8.5 Functions for integer arithmetic. Most of these are type-generic macros, but some also have function interfaces for specific types. \(x\) and \(y\) denote the arguments, \(\overline{x}\) is a bitwise complement, \(w\) the width, and \(m\) the maximum value of the type.
Function Description Remark
abs, labs, llabs \(|x|\) Type independent
div, ldiv, lldiv \(x\)/\(y\) and \(x\)%\(y\) Type independent
ckd_add \(x\)+\(y\) (LS bits) & overflow flag Target dependent
ckd_mul \(x\)*\(y\) (LS bits) & overflow flag Target dependent
ckd_sub \(x\)-\(y\) (LS bits) & overflow flag Target dependent
stdc_bit_ceil \(2^{\lceil \log_2 x\rceil}\) \(1\) if \(x \equiv 0\), \(0\) if \(x > m/2\)
stdc_bit_floor \(2^{\lfloor \log_2 x\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_bit_width \(1 + {\lfloor \log_2 x\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_count_ones Number of \(1\)-bits in \(x\) Type independent
stdc_count_zeros Number of \(1\)-bits in \(\overline{x}\) Type dependent
stdc_has_single_bit There is 𝑁 with \(x \equiv 2^n\) Type independent
stdc_first_leading_one \(w - {\big\lfloor \log_2 x\big\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_first_leading_zero \(w - {\big\lfloor \log_2 \overline{x}\big\rfloor}\) \(0\) if \(x\) has all bits set
stdc_leading_ones \(w - {\big\lfloor \log_2 \overline{x}\big\rfloor} - 1\) \(w\) if \(x \equiv 0\)
stdc_leading_zeros \(w - {\big\lfloor \log_2 x\big\rfloor} - 1\) \(w\) if \(x \equiv 0\)
stdc_first_trailing_one One plus LS \(1\)-bit in \(x\) \(0\) if \(x \equiv 0\)
stdc_first_trailing_zero One plus LS \(0\)-bit in \(x\) \(0\) if \(x \equiv 0\)
stdc_trailing_ones One plus LS \(0\)-bit in \(x\) \(w\) if \(x \equiv 0\)
stdc_trailing_zeros One plus LS \(1\)-bit in \(x\) \(w\) if \(x \equiv 0\)

Compare stdc_bit_width with related integer-arithmetic facilities, including bit-floor, bit-ceil, population-count, and single-bit tests. section 8.2

How Modern C, Third Edition covers it

How deep it goes
Defines the logarithmic result and zero case, but does not descend into implementation instructions or compiler lowering.
Examples
Uses the significant representation of 8 as 1000 to illustrate a result of 4 and compares neighboring integer functions.
What it assumes
Assumes familiarity with integer representations and bitwise operations while situating the facility in C23.
What you can do after
Apply it via <stdbit.h> to obtain significant-bit widths while leaving exceptional-value handling and optimization to the implementation.
How it connects
Connects stdc_bit_width with <stdbit.h>, stdc_bit_floor, stdc_count_ones, stdc_has_single_bit, and bitwise operators.

Related concepts

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