summaryrefslogtreecommitdiffstats
path: root/man/man3/powerof2.3
blob: c6e82d47e437b43ce0bea96841372464718bd5dd (plain)
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
.\" Copyright (C) 2022 Alejandro Colomar <alx@kernel.org>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH powerof2 3 2024-05-02 "Linux man-pages (unreleased)"
.SH NAME
powerof2 \- test if a value is a power of 2
.SH LIBRARY
Standard C library
.RI ( libc )
.SH SYNOPSIS
.nf
.B #include <sys/param.h>
.P
.BI "int powerof2(" x );
.fi
.SH DESCRIPTION
This macro returns true if
.I x
is a power of 2,
and false otherwise.
.P
.B 0
is considered a power of 2.
This can make sense considering wrapping of unsigned integers,
and has interesting properties.
.SH RETURN VALUE
True or false,
if
.I x
is a power of 2 or not,
respectively.
.SH STANDARDS
BSD.
.SH CAVEATS
The arguments may be evaluated more than once.
.P
Because this macro is implemented using bitwise operations,
some negative values can invoke undefined behavior.
For example,
the following invokes undefined behavior:
.IR "powerof2(INT_MIN);".
Call it only with unsigned types to be safe.
.SH SEE ALSO
.BR stdc_bit_ceil (3),
.BR stdc_bit_floor (3)