Blast-Radius Aware AI Code Review for Business-Critical Systems
LiveReview LiveReview Explore LiveReview

powerof2 - test if a value is a power of 2

Caveats

       The arguments may be evaluated more than once.

       Because this macro is implemented using bitwise operations, some negative  values  can  invoke  undefined
       behavior.   For example, the following invokes undefined behavior: powerof2(INT_MIN);.  Call it only with
       unsigned types to be safe.

Description

       This macro returns true if x is a power of 2, and false otherwise.

       0  is  considered  a  power of 2.  This can make sense considering wrapping of unsigned integers, and has
       interesting properties.

Library

       Standard C library (libc)

Name

       powerof2 - test if a value is a power of 2

Return Value

       True or false, if x is a power of 2 or not, respectively.

See Also

stdc_bit_ceil(3), stdc_bit_floor(3)

Linux man-pages 6.9.1                              2024-05-02                                        powerof2(3)

Standards

       BSD.

Synopsis

#include<sys/param.h>intpowerof2(x);

See Also