| 1 |
.\" Copyright, the authors of the Linux man-pages project |
| 2 |
.\" |
| 3 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 4 |
.\" |
| 5 |
.TH powerof2 3 2025-05-17 "Linux man-pages 6.18" |
| 6 |
.SH NAME |
| 7 |
powerof2 \- test if a value is a power of 2 |
| 8 |
.SH LIBRARY |
| 9 |
Standard C library |
| 10 |
.RI ( libc ) |
| 11 |
.SH SYNOPSIS |
| 12 |
.nf |
| 13 |
.B #include <sys/param.h> |
| 14 |
.P |
| 15 |
.BI "int powerof2(" x ); |
| 16 |
.fi |
| 17 |
.SH DESCRIPTION |
| 18 |
This macro returns true if |
| 19 |
.I x |
| 20 |
is a power of 2, |
| 21 |
and false otherwise. |
| 22 |
.P |
| 23 |
.B 0 |
| 24 |
is considered a power of 2. |
| 25 |
This can make sense considering wrapping of unsigned integers, |
| 26 |
and has interesting properties. |
| 27 |
.SH RETURN VALUE |
| 28 |
True or false, |
| 29 |
if |
| 30 |
.I x |
| 31 |
is a power of 2 or not, |
| 32 |
respectively. |
| 33 |
.SH STANDARDS |
| 34 |
BSD. |
| 35 |
.SH CAVEATS |
| 36 |
The arguments may be evaluated more than once. |
| 37 |
.P |
| 38 |
Because this macro is implemented using bitwise operations, |
| 39 |
some negative values can invoke undefined behavior. |
| 40 |
For example, |
| 41 |
the following invokes undefined behavior: |
| 42 |
.IR "powerof2(INT_MIN);". |
| 43 |
Call it only with unsigned types to be safe. |
| 44 |
.SH SEE ALSO |
| 45 |
.BR stdc_bit_ceil (3), |
| 46 |
.BR stdc_bit_floor (3) |