| Functions | ||||
stdc_leading_zeros (C23) | ||||
(C23) | ||||
(C23) | ||||
(C23) | ||||
(C23) | ||||
(C23) | ||||
(C23) | ||||
(C23) | ||||
(C23) | ||||
(C23) | ||||
| Macro constants | ||||
Defined in header <stdbit.h> | ||
unsignedint stdc_leading_zeros_uc(unsignedchar value)[[unsequenced]]; | (1) | (since C23) |
unsignedint stdc_leading_zeros_us(unsignedshort value)[[unsequenced]]; | (2) | (since C23) |
unsignedint stdc_leading_zeros_ui(unsignedint value)[[unsequenced]]; | (3) | (since C23) |
unsignedint stdc_leading_zeros_ul(unsignedlongint value)[[unsequenced]]; | (4) | (since C23) |
unsignedint stdc_leading_zeros_ull(unsignedlonglongint value)[[unsequenced]]; | (5) | (since C23) |
#define stdc_leading_zeros( value ) // exposed interface: | (6) | (since C23) |
generic_value_type argument) returns the appropriate value based on the type of the input value, so long as it is a:generic_return_type shall be a suitable large unsigned integer type capable of representing the computed result.Contents |
| value | - | value of unsigned integer type |
The number of consecutive0 bits in thevalue, starting from the most significant bit.
#include <limits.h>#include <stdbit.h>#include <stdint.h>#include <stdio.h> #define bits_num(value) (sizeof(value) * CHAR_BIT) #define bin_impl(T, suffix) \const char* bin_##suffix(T x) \{ \ static char buf[bits_num(x) * CHAR_BIT + 1]; \ for (T i = 0, mask = ((T)1 << (bits_num(x) - 1)); mask; mask >>= 1) \ buf[i++] = x & mask ? '1' : '0'; \ buf[bits_num(x)] = '\0'; \ return buf; \} bin_impl(uint8_t, u8)bin_impl(uint16_t, u16)bin_impl(uint32_t, u32)bin_impl(uint64_t, u64) #define bin(x) _Generic((x), \ uint8_t: bin_u8, uint16_t: bin_u16, uint32_t: bin_u32, default: bin_u64)(x) int main(){puts("uint8_t:");for(uint8_t x=0b11000000;; x>>=1){printf("x = [%s], leading zeros: %d\n", bin(x), stdc_leading_zeros(x));if(!x)break;} puts("uint16_t:");for(uint16_t x=0b11000000;; x>>=1){printf("x = [%s], leading zeros: %d\n", bin(x), stdc_leading_zeros(x));if(!x)break;}}
Output:
uint8_t:x = [11000000], leading zeros: 0x = [01100000], leading zeros: 1x = [00110000], leading zeros: 2x = [00011000], leading zeros: 3x = [00001100], leading zeros: 4x = [00000110], leading zeros: 5x = [00000011], leading zeros: 6x = [00000001], leading zeros: 7x = [00000000], leading zeros: 8uint16_t:x = [0000000011000000], leading zeros: 8x = [0000000001100000], leading zeros: 9x = [0000000000110000], leading zeros: 10x = [0000000000011000], leading zeros: 11x = [0000000000001100], leading zeros: 12x = [0000000000000110], leading zeros: 13x = [0000000000000011], leading zeros: 14x = [0000000000000001], leading zeros: 15x = [0000000000000000], leading zeros: 16
| finds the first position of0 bit, starting from the most significant bit (type-generic function macro)[edit] | |
(C23) | counts the number of0 bits in an unsigned integer (type-generic function macro)[edit] |
(C23) | counts the number of consecutive1 bits, starting from the most significant bit (type-generic function macro)[edit] |
C++ documentation forcountl_zero | |