stdc_count_ones(3)

stdc_count_ones3

Count the ones in an integer

Usage

Standard C Library (libc, -lc)

#include <stdbit.h>

unsigned int

stdc_count_ones_uc(unsigned char value);

unsigned int

stdc_count_ones_us(unsigned short value);

unsigned int

stdc_count_ones_ui(unsigned int value);

unsigned int

stdc_count_ones_ul(unsigned long value);

unsigned int

stdc_count_ones_ull(unsigned long long value);

unsigned int

stdc_count_ones(value);

Overview

The stdc_count_ones_type() family of functions returns the number of bits set to 1 in value, where value is of type unsigned char, unsigned short, unsigned int, unsigned long, or unsigned long long for type being uc, us, ui, ul, or ull respectively. This is also known as the “population count” (popcount) or “Hamming weight” operation. The stdc_count_ones(value) type-generic macro picks the appropriate stdc_count_ones_type() function based on the type of value.

Return values

Returns the number of bits set to 1 in value.

Releases

2 releases since FreeBSD 15.1-RELEASE; the text changed 1 time.

  1. 15.1
  2. 16.0

Standards

The stdc_count_ones_type() family of functions and the stdc_count_ones() type-generic macro conform to ISO/IEC 9899:2024 (“ISO C23”).

History

The stdc_count_ones_type() family of functions and the stdc_count_ones() type-generic macro were added in FreeBSD 15.1.

Authors

Robert Clausecker ⟨[email protected]⟩

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.