| STDC_HAS_SINGLE_BIT(3C) | Standard C Library Functions | STDC_HAS_SINGLE_BIT(3C) |
stdc_has_single_bit,
stdc_has_single_bit_uc,
stdc_has_single_bit_us,
stdc_has_single_bit_ui,
stdc_has_single_bit_ul,
stdc_has_single_bit_ull —
determine if only one bit is set
Standard C Library (libc, -lc)
#include
<stdbit.h>
bool
stdc_has_single_bit(generic_value_type
value);
bool
stdc_has_single_bit_uc(unsigned char
value);
bool
stdc_has_single_bit_us(unsigned short
value);
bool
stdc_has_single_bit_ui(unsigned int
value);
bool
stdc_has_single_bit_ul(unsigned long
value);
bool
stdc_has_single_bit_ull(unsigned long
long value);
The
stdc_has_single_bit()
family of functions determines whether the value has only a single bit set.
value. The function returns
true if there is exactly one bit whose value is set
to one in value.
The
stdc_has_single_bit()
function is generic and will operate on all 8, 16, 32, and 64-bit unsigned
integers; however, it is only available in C23. The other functions all
operate on a specific integer type, but otherwise behave the same and are
available regardless of the C language version.
The functions in the stdc_has_single_bit()
family return true if exactly one bit is set in
value. Otherwise, false is
returned. These functions cannot fail.
Example 1 Printing whether only a single bit is set.
#include <stdbit.h>
#include <stdio.h>
#include <limits.h>
int
main(void)
{
printf("%s %s %s %s\n",
stdc_has_single_bit_uc(0x23) ? "true" : "false",
stdc_has_single_bit_us(0x0080) ? "true" : "false",
stdc_has_single_bit_ui(0x81941b23) ? "true" : "false",
stdc_has_single_bit_ull(0x00200000000000000) ?
"true" : "false");
return (0);
}
When compiled and run, this produces:
$ ./a.out false true false true
stdc_bit_ceil(3C), stdc_bit_floor(3C), stdc_bit_width(3C), stdc_count_ones(3C), stdc_count_zeros(3C), stdc_first_leading_one(3C), stdc_first_leading_zero(3C), stdc_first_trailing_one(3C), stdc_first_trailing_zero(3C), stdc_leading_ones(3C), stdc_leading_zeros(3C), stdc_trailing_ones(3C), stdc_trailing_zeros(3C), stdbit.h(3HEAD)
| October 27, 2024 | OmniOS |