xref: /freebsd/lib/libc/stdbit/stdc_count_ones.3 (revision d790b16bbf0c3055ef00200d01659dfb3c0e5e5e)
1.\"
2.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
3.\"
4.\" SPDX-License-Identifier: BSD-2-Clause
5.\"
6.Dd November 9, 2025
7.Dt STDC_COUNT_ONES 3
8.Os
9.Sh NAME
10.Nm stdc_count_ones
11.Nd count the ones in an integer
12.Sh SYNOPSIS
13.Lb libc
14.In stdbit.h
15.Ft unsigned int
16.Fn stdc_count_ones_uc "unsigned char value"
17.Ft unsigned int
18.Fn stdc_count_ones_us "unsigned short value"
19.Ft unsigned int
20.Fn stdc_count_ones_ui "unsigned int value"
21.Ft unsigned int
22.Fn stdc_count_ones_ul "unsigned long value"
23.Ft unsigned int
24.Fn stdc_count_ones_ull "unsigned long long value"
25.Ft unsigned int
26.Fn stdc_count_ones "value"
27.Sh DESCRIPTION
28The
29.Nm stdc_count_ones_ Ns Em type Ns ()
30family of functions returns the number of bits set to\~1 in
31.Fa value ,
32where
33.Fa value
34is of type
35.Va unsigned char ,
36.Va unsigned short ,
37.Va unsigned int ,
38.Va unsigned long ,
39or
40.Va unsigned long long
41for
42.Em type
43being
44.Sy uc ,
45.Sy us ,
46.Sy ui ,
47.Sy ul ,
48or
49.Sy ull
50respectively.
51This is also known as the
52.Dq population count
53(popcount) or
54.Dq Hamming weight
55operation.
56The
57.Fn stdc_count_ones "value"
58type-generic macro picks the appropriate
59.Nm stdc_count_ones_ Ns Em type Ns ()
60function based on the type of
61.Fa value .
62.Sh RETURN VALUES
63Returns the number of bits set to\~1 in
64.Fa value .
65.Sh SEE ALSO
66.Xr bit_count 3 ,
67.Xr stdbit 3 ,
68.Xr stdc_count_zeros 3 ,
69.Xr stdc_has_single_bit 3
70.Sh STANDARDS
71The
72.Nm stdc_count_ones_ Ns Em type Ns ()
73family of functions and the
74.Fn stdc_count_ones
75type-generic macro conform to
76.St -isoC-2023 .
77.Sh HISTORY
78The
79.Nm stdc_count_ones_ Ns Em type Ns ()
80family of functions and the
81.Fn stdc_count_ones
82type-generic macro were added in
83.Fx 15.1.
84.Sh AUTHOR
85.An Robert Clausecker Aq Mt fuz@FreeBSD.org
86