powerof2(3)

πŸ’– powerof2 β€” bestie, is this number a power of 2? πŸ’–

okay so real talk, this is powerof2 from the Library Functions Manual, living in the Standard C library (libc) β€” she's a tiny little macro and she has ONE job and she does it slayfully πŸ’…


✨ SYNOPSIS ✨

#include <sys/param.h>

int powerof2(x);

yeah that's it. that's the whole vibe. one include, one macro, no drama πŸŽ€


🌸 DESCRIPTION 🌸

so powerof2 is a macro that returns true if x is a power of 2, and false otherwise. iconic, honestly. no cap.

and now the plot twist that makes everyone gasp: 0 is considered a power of 2. yes fr fr, zero, the number that gives everyone trust issues, counts. why? because when you think about wrapping of unsigned integers it actually makes sense, and it has interesting properties β€” the manpage said that, not me, but I'm obsessed with how mysterious she's being about it πŸ¦‹


πŸ’« RETURN VALUE πŸ’«

True if x is a power of 2, false if it isn't. respectively, like the manpage said. that's the whole answer. periodt.


πŸ‘‘ STANDARDS πŸ‘‘

BSD. that's it, that's the standard. short king behavior.


😭 CAVEATS (read this part please, i'm begging) 😭

  • the arguments may be evaluated more than once. so like, if you're passing something with side effects, she might run it a couple times and you'll be like ?????? so don't do that bestie.
  • because this macro is implemented using bitwise operations, some negative values can invoke undefined behavior β€” and undefined behavior is the final boss, we do NOT fight her. for example, this is UB:
powerof2(INT_MIN);
πŸ’€ literally do not do that. the manpage says to call it only with unsigned types to be safe, so that's what we're gonna do. unsigned only. pinky promise. 🫢

🫢 SEE ALSO 🫢

go read those too, they're her cousins and they also ate πŸ’–


the cute lil sign-off: this page came to you from Linux man-pages 6.20, dated 2025-05-17 β€” so she's recent, she's fresh, she's gorgeous, and now you know her. go forth and check your powers of 2 responsibly βœ¨πŸŽ€πŸŒˆπŸ’œ

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms