rulururu

post Empty blocks

October 27th, 2008

Filed under: C++ — Kai @ 10:54 am

A tricky loop I’d like to show you.

First of all a quick explanation of the two functions I use.

  • log2 computes the base-2 logarithm of x.
  • ceil rounds, it returns the smallest integral value that is not less than x.
/* compute the ceil(log2(x)); i,x are unsigned; x is not 0 */
for( i = x>>1, n = 0; i != 0; i >>= 1, n++ ) {
}

In C we can have loops that have nothing in their body. You should use braces around an empty body. This allows you to expand the body if necessary. Further it gives us a visual clue that something out-of-the-normal is going on.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)