about summary refs log tree commit diff
path: root/manual/stdbit.texi
blob: 48591eab8229cc03b8adc3ed2115dbefa6103d7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
@node Bit Manipulation, Date and Time, Arithmetic, Top
@c %MENU% Bit manipulation
@chapter Bit Manipulation

This chapter contains information about functions and macros for
determining the endianness of integer types and manipulating the bits
of unsigned integers.  These functions and macros are from ISO C23 and
are declared in the header file @file{stdbit.h}.

The following macros describe the endianness of integer types.  They
have values that are integer constant expressions.

@defmac __STDC_ENDIAN_LITTLE__
This macro represents little-endian storage.
@end defmac

@defmac __STDC_ENDIAN_BIG__
This macro represents big-endian storage.
@end defmac

@defmac __STDC_ENDIAN_NATIVE__
This macro equals @code{__STDC_ENDIAN_LITTLE__} if integer types are
stored in memory in little-endian format, and equals
@code{__STDC_ENDIAN_BIG__} if integer types are stored in memory in
big-endian format.
@end defmac

The following functions manipulate the bits of unsigned integers.
Each function family has functions for the types @code{unsigned char},
@code{unsigned short}, @code{unsigned int}, @code{unsigned long int}
and @code{unsigned long long int}.  In addition, there is a
corresponding type-generic macro (not listed below), named the same as
the functions but without any suffix such as @samp{_uc}.  The
type-generic macro can only be used with an argument of an unsigned
integer type with a width of 8, 16, 32 or 64 bits, or when using
a compiler with support for
@uref{https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html,@code{__builtin_stdc_bit_ceil}},
etc.@:, built-in functions such as GCC 14.1 or later
any unsigned integer type those built-in functions support.
In GCC 14.1 that includes support for @code{unsigned __int128} and
@code{unsigned _BitInt(@var{n})} if supported by the target.

@deftypefun {unsigned int} stdc_leading_zeros_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_leading_zeros_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_leading_zeros_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_leading_zeros_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_leading_zeros_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_leading_zeros} functions count the number of leading
(most significant) zero bits in @var{x}, starting from the most
significant bit of the argument type.  If @var{x} is zero, they return
the width of @var{x} in bits.
@end deftypefun

@deftypefun {unsigned int} stdc_leading_ones_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_leading_ones_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_leading_ones_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_leading_ones_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_leading_ones_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_leading_ones} functions count the number of leading
(most significant) one bits in @var{x}, starting from the most
significant bit of the argument type.
@end deftypefun

@deftypefun {unsigned int} stdc_trailing_zeros_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_trailing_zeros_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_trailing_zeros_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_trailing_zeros_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_trailing_zeros_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_trailing_zeros} functions count the number of trailing
(least significant) zero bits in @var{x}, starting from the least
significant bit of the argument type.  If @var{x} is zero, they return
the width of @var{x} in bits.
@end deftypefun

@deftypefun {unsigned int} stdc_trailing_ones_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_trailing_ones_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_trailing_ones_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_trailing_ones_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_trailing_ones_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_trailing_ones} functions count the number of trailing
(least significant) one bits in @var{x}, starting from the least
significant bit of the argument type.
@end deftypefun

@deftypefun {unsigned int} stdc_first_leading_zero_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_first_leading_zero_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_first_leading_zero_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_first_leading_zero_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_first_leading_zero_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_first_leading_zero} functions return the position of
the most significant zero bit in @var{x}, counting from the most
significant bit of @var{x} as 1, or zero if there is no zero bit in
@var{x}.
@end deftypefun

@deftypefun {unsigned int} stdc_first_leading_one_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_first_leading_one_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_first_leading_one_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_first_leading_one_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_first_leading_one_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_first_leading_one} functions return the position of the
most significant one bit in @var{x}, counting from the most
significant bit of @var{x} as 1, or zero if there is no one bit in
@var{x}.
@end deftypefun

@deftypefun {unsigned int} stdc_first_trailing_zero_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_zero_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_zero_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_zero_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_zero_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_first_trailing_zero} functions return the position of
the least significant zero bit in @var{x}, counting from the least
significant bit of @var{x} as 1, or zero if there is no zero bit in
@var{x}.
@end deftypefun

@deftypefun {unsigned int} stdc_first_trailing_one_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_one_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_one_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_one_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_first_trailing_one_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_first_trailing_one} functions return the position of
the least significant one bit in @var{x}, counting from the least
significant bit of @var{x} as 1, or zero if there is no one bit in
@var{x}.
@end deftypefun

@deftypefun {unsigned int} stdc_count_zeros_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_count_zeros_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_count_zeros_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_count_zeros_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_count_zeros_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_count_zeros} functions count the number of zero bits in
@var{x}.
@end deftypefun

@deftypefun {unsigned int} stdc_count_ones_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_count_ones_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_count_ones_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_count_ones_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_count_ones_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_count_ones} functions count the number of one bits in
@var{x}.
@end deftypefun

@deftypefun {_Bool} stdc_has_single_bit_uc (unsigned char @var{x})
@deftypefunx {_Bool} stdc_has_single_bit_us (unsigned short @var{x})
@deftypefunx {_Bool} stdc_has_single_bit_ui (unsigned int @var{x})
@deftypefunx {_Bool} stdc_has_single_bit_ul (unsigned long int @var{x})
@deftypefunx {_Bool} stdc_has_single_bit_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_has_single_bit} functions return whether @var{x} has
exactly one bit set to one.
@end deftypefun

@deftypefun {unsigned int} stdc_bit_width_uc (unsigned char @var{x})
@deftypefunx {unsigned int} stdc_bit_width_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_bit_width_ui (unsigned int @var{x})
@deftypefunx {unsigned int} stdc_bit_width_ul (unsigned long int @var{x})
@deftypefunx {unsigned int} stdc_bit_width_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_bit_width} functions return the minimum number of bits
needed to store @var{x}, not counting leading zero bits.  If @var{x}
is zero, they return zero.
@end deftypefun

@deftypefun {unsigned char} stdc_bit_floor_uc (unsigned char @var{x})
@deftypefunx {unsigned short} stdc_bit_floor_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_bit_floor_ui (unsigned int @var{x})
@deftypefunx {unsigned long int} stdc_bit_floor_ul (unsigned long int @var{x})
@deftypefunx {unsigned long long int} stdc_bit_floor_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_bit_floor} functions return the largest integer power
of two that is less than or equal to @var{x}.  If @var{x} is zero,
they return zero.
@end deftypefun

@deftypefun {unsigned char} stdc_bit_ceil_uc (unsigned char @var{x})
@deftypefunx {unsigned short} stdc_bit_ceil_us (unsigned short @var{x})
@deftypefunx {unsigned int} stdc_bit_ceil_ui (unsigned int @var{x})
@deftypefunx {unsigned long int} stdc_bit_ceil_ul (unsigned long int @var{x})
@deftypefunx {unsigned long long int} stdc_bit_ceil_ull (unsigned long long int @var{x})
@standards{C23, stdbit.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
The @code{stdc_bit_ceil} functions return the smallest integer power
of two that is greater than or equal to @var{x}.  If this cannot be
represented in the return type, they return zero.
@end deftypefun