trinity-plugins/kmer/libutil/bitOperations.h
inline
u32bit
reverseBits32(u32bit x) {
  x = ((x >>  1) & u32bitNUMBER(0x55555555)) | ((x <<  1) & u32bitNUMBER(0xaaaaaaaa));
  x = ((x >>  2) & u32bitNUMBER(0x33333333)) | ((x <<  2) & u32bitNUMBER(0xcccccccc));
  x = ((x >>  4) & u32bitNUMBER(0x0f0f0f0f)) | ((x <<  4) & u32bitNUMBER(0xf0f0f0f0));
  x = ((x >>  8) & u32bitNUMBER(0x00ff00ff)) | ((x <<  8) & u32bitNUMBER(0xff00ff00));
  x = ((x >> 16) & u32bitNUMBER(0x0000ffff)) | ((x << 16) & u32bitNUMBER(0xffff0000));
  return(x);
}
//  I wish I could claim these.
//
//  Freed, Edwin E. 1983. "Binary Magic Number" Dr. Dobbs Journal
//  Vol. 78 (April) pp. 24-37
//
//  Supposedly tells us how to reverse the bits in a word, count the number
//  of set bits in a words and more.
//
//  A bit of verbage on counting the number of set bits.  The naive way
//  is to loop and shift:
//
//      u32bit r = u32bitZERO;
//      while (x) {
//        r++;
//        x >>= 1;
//      }
//      return(r);
//
//  http://remus.rutgers.edu/~rhoads/Code/bitcount3.c has an optimized
//  method:
//
//      x -= (0xaaaaaaaa & x) >> 1;
//      x  = (x & 0x33333333) + ((x >> 2) & 0x33333333);
//      x += x >> 4;
//      x &= 0x0f0f0f0f;
//      x += x >> 8;
//      x += x >> 16;
//      x &= 0x000000ff;
//      return(x);
//
//  No loops!
//
//  Freed's methods are easier to understand, and just as fast.
//
//  Using our bit counting routines, Ross Lippert suggested a nice
//  way of computing log2 -- use log2 shifts to fill up the lower
//  bits, then count bits.  See logBaseTwo*()
//
I wish I could claim these.
Freed, Edwin E. 1983. "Binary Magic Number" Dr. Dobbs Journal
Vol.

Supposedly tells us how to reverse the bits in a word, count the number
of set bits in a words and more.

A bit of verbage on counting the number of set bits.  The naive way
is to loop and shift.







No loops!

Freed's methods are easier to understand, and just as fast.

Using our bit counting routines, Ross Lippert suggested a nice
way of computing log2 -- use log2 shifts to fill up the lower
bits, then count bits.
I wish I could claim these . Freed Edwin E . 1983 . " Binary Magic Number " Dr . Dobbs Journal Vol . Supposedly tells us how to reverse the bits in a word count the number of set bits in a words and more . A bit of verbage on counting the number of set bits . The naive way is to loop and shift . No loops! Freed ' s methods are easier to understand and just as fast . Using our bit counting routines Ross Lippert suggested a nice way of computing log2 -- use log2 shifts to fill up the lower bits then count bits .
inline
u32bit
reverseBits32(u32bit x) {
  x = ((x >>  1) & u32bitNUMBER(0x55555555)) | ((x <<  1) & u32bitNUMBER(0xaaaaaaaa));
  x = ((x >>  2) & u32bitNUMBER(0x33333333)) | ((x <<  2) & u32bitNUMBER(0xcccccccc));
  x = ((x >>  4) & u32bitNUMBER(0x0f0f0f0f)) | ((x <<  4) & u32bitNUMBER(0xf0f0f0f0));
  x = ((x >>  8) & u32bitNUMBER(0x00ff00ff)) | ((x <<  8) & u32bitNUMBER(0xff00ff00));
  x = ((x >> 16) & u32bitNUMBER(0x0000ffff)) | ((x << 16) & u32bitNUMBER(0xffff0000));
  return(x);
}
inline u32bit reverseBits32 ( u32bit x ) { x = ( ( x >> 1 ) & u32bitNUMBER ( 0x55555555 ) ) | ( ( x << 1 ) & u32bitNUMBER ( 0xaaaaaaaa ) ) ; x = ( ( x >> 2 ) & u32bitNUMBER ( 0x33333333 ) ) | ( ( x << 2 ) & u32bitNUMBER ( 0xcccccccc ) ) ; x = ( ( x >> 4 ) & u32bitNUMBER ( 0x0f0f0f0f ) ) | ( ( x << 4 ) & u32bitNUMBER ( 0xf0f0f0f0 ) ) ; x = ( ( x >> 8 ) & u32bitNUMBER ( 0x00ff00ff ) ) | ( ( x << 8 ) & u32bitNUMBER ( 0xff00ff00 ) ) ; x = ( ( x >> 16 ) & u32bitNUMBER ( 0x0000ffff ) ) | ( ( x << 16 ) & u32bitNUMBER ( 0xffff0000 ) ) ; return ( x ) ; }
I wish I could claim these.
I wish I could claim these .