xref: /freebsd/share/man/man3/stdbit.3 (revision d39e310c7d6a42b459aa7919310e3c99cc478d50)
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 STDBIT 3
8.Os
9.Sh NAME
10.Nm stdbit
11.Nd bit and byte utilities
12.Sh SYNOPSIS
13.Lb libc
14.In stdbit.h
15.Fd #define __STDC_ENDIAN_LITTLE__
16.Fd #define __STDC_ENDIAN_BIG__
17.Fd #define __STDC_ENDIAN_NATIVE__
18.Ft unsigned int
19.Fn stdc_count_leading_zeros "value"
20.Ft unsigned int
21.Fn stdc_count_leading_ones "value"
22.Ft unsigned int
23.Fn stdc_count_trailing_zeros "value"
24.Ft unsigned int
25.Fn stdc_count_trailing_ones "value"
26.Ft unsigned int
27.Fn stdc_first_leading_zero "value"
28.Ft unsigned int
29.Fn stdc_first_leading_one "value"
30.Ft unsigned int
31.Fn stdc_first_trailing_zero "value"
32.Ft unsigned int
33.Fn stdc_first_trailing_one "value"
34.Ft unsigned int
35.Fn stdc_count_zeros "value"
36.Ft unsigned int
37.Fn stdc_count_ones "value"
38.Ft bool
39.Fn stdc_has_single_bit "value"
40.Ft unsigned int
41.Fn stdc_bit_width "value"
42.Ft typeof Ns Pq Em value
43.Fn stdc_bit_floor "value"
44.Ft typeof Ns Pq Em value
45.Fn stdc_bit_ceil "value"
46.Sh DESCRIPTION
47The
48.Dv __STDC_ENDIAN_NATIVE__
49macro describes the byte order or endianness of the machine for which the
50program is built.
51If the machine has big-endian byte order, this macro is equal to
52.Dv __STDC_ENDIAN_BIG__ .
53If the machine has little-endian byte order, this macro is equal to
54.Dv __STDC_ENDIAN_LITTLE__ .
55Otherwise, the macro has a value that is equal to neither.
56.Pp
57The bit and byte utility functions analyze the bits within a datum.
58Each function
59.Em func
60is provided in five variants
61.Nm stdc_ Ns Em func Ns Em _ Ns Em type Ns Pq Em value
62where
63.Fa value
64is of type
65.Va unsigned char ,
66.Va unsigned short ,
67.Va unsigned int ,
68.Va unsigned long ,
69or
70.Va unsigned long long
71for
72.Em type
73being
74.Sy uc ,
75.Sy us ,
76.Sy ui ,
77.Sy ul ,
78or
79.Sy ull
80respectively.
81Additionally, for each
82.Em func ,
83a type-generic macro
84.Nm stdc_ Ns Em func Ns Pq Em value
85that picks the appropriate function
86.Nm stdc_ Ns Em func Ns Em _ Ns Em type Ns Pq Em value
87based on the type of
88.Fa value
89is provided.
90.Sh SEE ALSO
91.Xr arch 7 ,
92.Xr bitstring 3 ,
93.Xr ffs 3 ,
94.Xr fls 3 ,
95.Xr stdc_count_leading_zeros 3 ,
96.Xr stdc_count_leading_ones 3 ,
97.Xr stdc_count_trailing_zeros 3 ,
98.Xr stdc_count_trailing_ones 3 ,
99.Xr stdc_first_leading_zero 3 ,
100.Xr stdc_first_leading_one 3 ,
101.Xr stdc_first_trailing_zero 3 ,
102.Xr stdc_first_trailing_one 3 ,
103.Xr stdc_count_zeros 3 ,
104.Xr stdc_count_ones 3 ,
105.Xr stdc_has_single_bit 3 ,
106.Xr stdc_bit_width 3 ,
107.Xr stdc_bit_floor 3 ,
108.Xr stdc_bit_ceil 3
109.Sh STANDARDS
110The macros and functions of the
111.In stdbit.h
112header conform to
113.St -isoC-2023 .
114.Sh HISTORY
115The
116.In stdbit.h
117header and the macros and functions defined therein where added in
118.Fx 15.1.
119.Sh AUTHOR
120.Ah Robert Clausecker Aq Mt fuz@FreeBSD.org
121