private static int interleaveBits ( int odd , int even ) { int val = 0 ; // Replaced this line with the improved code provided by Tuska // int n = Math.max(Integer.highestOneBit(odd), Integer.highestOneBit(even)); int max = Math . max ( odd , even ) ; int n = 0 ; while ( max > 0 ) { n ++ ; max >>= 1 ; } for ( int i = 0 ; i < n ; i ++ ) { int bitMask = 1 << i ; int a = ( even & bitMask ) > 0 ? ( 1 << ( 2 * i ) ) : 0 ; int b = ( odd & bitMask ) > 0 ? ( 1 << ( 2 * i + 1 ) ) : 0 ; val += a + b ; } return val ; }
Interleave the bits from two input integer values