Next: Range of an Integer Type, Up: Data Type Measurements [Contents][Index]
The most common reason that a program needs to know how many bits are in
an integer type is for using an array of long int
as a bit vector.
You can access the bit at index n with
vector[n / LONGBITS] & (1 << (n % LONGBITS))
provided you define LONGBITS
as the number of bits in a
long int
.
There is no operator in the C language that can give you the number of
bits in an integer data type. But you can compute it from the macro
CHAR_BIT
, defined in the header file limits.h.
CHAR_BIT
This is the number of bits in a char
—eight, on most systems.
The value has type int
.
You can compute the number of bits in any data type type like this:
sizeof (type) * CHAR_BIT