News for hackers. Stuff that matters.
Re:Ten Ways to Check if an Integer Is a Power Of Two in C by bad_user on Tuesday July 26, 2011 @11:24AM (#2807747)

     return ((x != 0) && !(x & (x - 1)));
This is beautiful.
Re:Ten Ways to Check if an Integer Is a Power Of Two in C by gjm11 on Tuesday July 26, 2011 @03:24PM (#2809062)
Unless x==0 is often true, it's probably better to put the conjuncts in the other order:

  return (!(x & (x-1)) && (x != 0));
because that way you don't have to test x against 0 so often.

(On my machine it appears to be about 10% faster.)

[EDIT to clarify: 10% faster with the particular sample of x values that I tested, which happened to be all the integers from 0 up to 2^30-1 once each. Of course if you only ever call it with x=0 then the original version will be faster. Also, if this is really in your inner loop then you're probably doing something wrong :-).]

Re:Ten Ways to Check if an Integer Is a Power Of Two in C by vog on Wednesday July 27, 2011 @09:26AM (#2812002)
However, this way your code enters an undefined state in C, strictly speaken. (as far as I unstand the standard)

So theoretically, an extremely aggressive optimizer would be allowed to generate machine code that doesn't handle the case x==0 properly.

If (x != 0) is placed first, the optimizer wouldn't be allowed to do that, due to short-circuit evaluation.

Re:Ten Ways to Check if an Integer Is a Power Of Two in C by gjm11 on Wednesday July 27, 2011 @07:35PM (#2814738)
It's OK if x is of an unsigned integer type. If x is of a signed type, though, you're right: strictly, the value of x-1 is then undefined when x==0.

(In practice, of course, it's perfectly safe unless you're on a distinctly exotic system, and if you are then you probably know you are.)

Re:Ten Ways to Check if an Integer Is a Power Of Two in C by vog on Thursday July 28, 2011 @10:43AM (#2816971)
I disagree.

Why should x-1 for x==0 be undefined for a signed type? The signed type can happily represent -1. Rather, x-1 is undefined for unsigned types because those have no representation of -1.

Re:Ten Ways to Check if an Integer Is a Power Of Two in C by gjm11 on Thursday July 28, 2011 @12:49PM (#2817670)
Yow, what the hell was I thinking when I wrote that? Let me try again.

Preliminary note: I'm looking at a draft version of the C9X standard, because that's what I have to hand.

When x is of an unsigned type, it's OK. (In particular, arithmetic overflow on unsigned types is defined to work modulo (max value + 1).)

When x is of a signed type, the bit-patterns corresponding to negative numbers are left somewhat up for grabs. It has to be either sign+magnitude, or 1's-complement, or 2's-complement. (I'm pretty sure this was not true in earlier versions of the standard.)

However, the result of applying a bitwise logical operator to a negative value is defined to be what you get by applying that logical operation to the bits. In other words, it's undefined (actually, unspecified, which isn't quite the same) what answer you get, but this isn't one of those cases where the standard permits implementations to cause the Moon to explode or pornographic email to be sent to your employer.

In particular, whatever value x&(x-1) has when x==0, it has a value and the whole expression (including the && (x!=0) bit) comes out false regardless.

So, I take back my earlier statement: according to my reading of (a draft of) the (latest) C standard, my version is in fact guaranteed to do the right thing in all implementations, even when x is of signed type.

I repeat that I haven't looked at earlier versions of the standard; I think they were less specific about how signed integer types can be represented, and I wouldn't be surprised if bitwise ops on negative numbers (at least) had entirely undefined behaviour then.

Re:Ten Ways to Check if an Integer Is a Power Of Two in C by lojack on Tuesday July 26, 2011 @02:25PM (#2808733)
Check out Bit Twiddling Hacks if you'd like to see more: http://graphics.stanford.edu/~seander/bithacks.html
Re:Ten Ways to Check if an Integer Is a Power Of Two in C by slug on Tuesday July 26, 2011 @11:35AM (#2807799)
for more equally beautiful expressions, check out "Hacker's Delight".
Re:Ten Ways to Check if an Integer Is a Power Of Two in C by MostAwesomeDude on Tuesday July 26, 2011 @12:28PM (#2808102)
And fast; Mesa uses this.

  static INLINE boolean
  util_is_power_of_two( unsigned v )
  {
     return (v & (v-1)) == 0;
  }
Re:Ten Ways to Check if an Integer Is a Power Of Two in C by ColinWright on Tuesday July 26, 2011 @01:10PM (#2808363)
Returns True when v==0, and yet 0 is not a power of 2.

Further, this technique - correctly applied - is essentially #9 in the list.

Re:Ten Ways to Check if an Integer Is a Power Of Two in C by vog on Wednesday July 27, 2011 @09:30AM (#2812019)
According to the C spec, this is expression is undefined for v == 0.

So it might return true, or return false, or start NetHack, or wipe your disk. In other words: Strictly speaken, that function is not allowed to be called for v == 0.

Re:Ten Ways to Check if an Integer Is a Power Of Two in C by MostAwesomeDude on Thursday July 28, 2011 @09:39AM (#2816714)
Mesa is a GL renderer. POT only matters in 3D for things like texture sizes. Textures aren't allowed to have zero-sized dimensions. The check for v == 0 occurs far higher up the stack than where this function's used.

We're not writing bad code, we promise. :3