C Reference ยท Keywords and Statements
C _BitInt bit-precise integers
Reference for the C23 _BitInt(N) type: integers with an exact width in bits, the wb and uwb literal suffixes, promotion rules, limits and portability notes.
_BitInt(N) is a C23 integer type whose width in bits you choose yourself. _BitInt(7) has exactly seven value bits plus a sign bit, unsigned _BitInt(12) has twelve value bits. Arithmetic on these types wraps (for unsigned) or overflows (for signed) at the width you picked, not at the width of int.
The type exists for hardware description, cryptography, protocol fields and any place where a number must have an exact size, including sizes far beyond 64 bits. Unlike int, a _BitInt is never silently promoted to int in arithmetic, so there are fewer surprises.
_BitInt(24) sample = 100wb; unsigned _BitInt(128) big = 1uwb << 100;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
_BitInt(N) unsigned _BitInt(N) signed _BitInt(N)
| Part | Description |
|---|---|
N |
An integer constant expression. For unsigned, at least 1; for signed (C23), at least 2. At most BITINT_MAXWIDTH from <limits.h>. |
wb, WB |
Literal suffix for a signed _BitInt just wide enough for the value (plus sign bit). |
uwb, UWB |
Literal suffix for an unsigned _BitInt just wide enough for the value. |
BITINT_MAXWIDTH |
Largest supported N; always at least ULLONG_WIDTH. |
A plain _BitInt(N) is signed. It is never the same type as int, long or char, even if the widths match.
Errors / undefined behavior: signed overflow is undefined, as for int. Shifting by a count at least as large as N is undefined. A width larger than BITINT_MAXWIDTH or a constant that is not a valid N is a compile error. _BitInt cannot be the underlying type of an enum, and it cannot be used as a bit-field with a mismatched width.
Usage notes
- No integer promotion. If both operands are
_BitInt, the result has the larger width; a narrow_BitIntis not widened tointfirst. When you mix a_BitIntwith a standard integer type, the usual arithmetic conversions pick the wider of the two ranks, so a_BitInt(12)plus anintis anint. - Unsigned wraps at N bits, which makes
unsigned _BitInt(8)a precise stand-in for a hardware byte register. printfhas no_BitIntconversion in C23. Cast to a standard type that is wide enough, or print in pieces as above.sizeofis padded. An_BitInt(24)normally occupies 4 bytes; the width counts value bits, not storage.BITINT_MAXWIDTHis large on GCC and Clang (thousands of bits), but is only guaranteed to be 64.- Not a big-number library. Multiplication and division of huge widths work, but they are slow compared with fixed-size hardware types. Choose the smallest width that fits.
- ABI caveat: the calling convention for wide
_BitIntvalues varies by platform; do not expose them in a shared-library interface yet. - Added in C23. Compilers accept it in
-std=c23mode, GCC 14 and later on most targets.