STDC_LEADING_ONES(3C) Standard C Library Functions STDC_LEADING_ONES(3C)
stdc_leading_ones, stdc_leading_ones_uc, stdc_leading_ones_us,
stdc_leading_ones_ui, stdc_leading_ones_ul, stdc_leading_ones_ull -
count consecutive leading one bits
Standard C Library (libc, -lc)
#include <stdbit.h>
unsigned int
stdc_leading_ones(generic_value_type value);
unsigned int
stdc_leading_ones_uc(unsigned char value);
unsigned int
stdc_leading_ones_us(unsigned short value);
unsigned int
stdc_leading_ones_ui(unsigned int value);
unsigned int
stdc_leading_ones_ul(unsigned long value);
unsigned int
stdc_leading_ones_ull(unsigned long long value);
The stdc_leading_ones() family of functions counts the number of
consecutive one bits present in value starting at the most significant
bit.
The stdc_leading_ones() 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_leading_ones() family always return the
number of leading ones found in value. These functions cannot fail.
Example 1 Printing the number of leading zeros.
#include <stdbit.h>
#include <stdio.h>
#include <limits.h>
int
main(void)
{
printf("0x%x 0x%x 0x%x 0x%x\n", stdc_leading_ones_uc(0xf6),
stdc_leading_ones_us(0x9009), stdc_leading_ones_ui(UINT32_MAX),
stdc_leading_ones_ull(0));
return (0);
}
When compiled and run, this produces:
$ ./a.out
0x4 0x1 0x20 0x0
Committed
MT-LEVEL
Async-Signal-Safe
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_has_single_bit(3C),
stdc_leading_zeros(3C), stdc_trailing_ones(3C),
stdc_trailing_zeros(3C), stdbit.h(3HEAD)
illumos October 27, 2024 illumos
NAME
stdc_leading_ones, stdc_leading_ones_uc, stdc_leading_ones_us,
stdc_leading_ones_ui, stdc_leading_ones_ul, stdc_leading_ones_ull -
count consecutive leading one bits
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <stdbit.h>
unsigned int
stdc_leading_ones(generic_value_type value);
unsigned int
stdc_leading_ones_uc(unsigned char value);
unsigned int
stdc_leading_ones_us(unsigned short value);
unsigned int
stdc_leading_ones_ui(unsigned int value);
unsigned int
stdc_leading_ones_ul(unsigned long value);
unsigned int
stdc_leading_ones_ull(unsigned long long value);
DESCRIPTION
The stdc_leading_ones() family of functions counts the number of
consecutive one bits present in value starting at the most significant
bit.
The stdc_leading_ones() 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.
RETURN VALUES
The functions in the stdc_leading_ones() family always return the
number of leading ones found in value. These functions cannot fail.
EXAMPLES
Example 1 Printing the number of leading zeros.
#include <stdbit.h>
#include <stdio.h>
#include <limits.h>
int
main(void)
{
printf("0x%x 0x%x 0x%x 0x%x\n", stdc_leading_ones_uc(0xf6),
stdc_leading_ones_us(0x9009), stdc_leading_ones_ui(UINT32_MAX),
stdc_leading_ones_ull(0));
return (0);
}
When compiled and run, this produces:
$ ./a.out
0x4 0x1 0x20 0x0
INTERFACE STABILITY
Committed
MT-LEVEL
Async-Signal-Safe
SEE ALSO
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_has_single_bit(3C),
stdc_leading_zeros(3C), stdc_trailing_ones(3C),
stdc_trailing_zeros(3C), stdbit.h(3HEAD)
illumos October 27, 2024 illumos