Re: C macro to find the next power of 2 ?

From: Bakul Shah <bakul_at_bitblocks.com>
Date: Wed, 07 Feb 2007 02:01:01 -0800
> 	#define b2(x)   (   (x) | (   (x) >> 1) )
> 	#define b4(x)   ( b2(x) | ( b2(x) >> 2) )
> 	#define b8(x)   ( b4(x) | ( b4(x) >> 4) )
> 	#define b16(x)  ( b8(x) | ( b8(x) >> 8) )
> 	#define b32(x)  (b16(x) | (b16(x) >>16) )
> 	#define next_power_of_2(x)      (b32(x-1) + 1)
...
> But i was wondering if there is some simpler code which
> can still be computed as a compile-time constant ?

If this works, why bother?  let the compiler work harder!
Received on Wed Feb 07 2007 - 09:25:29 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:05 UTC