public void reverse ( ) { int [ ] newBits = new int [ bits . length ] ; // reverse all int's first int len = ( size - 1 ) / 32 ; int oldBitsLen = len + 1 ; for ( int i = 0 ; i < oldBitsLen ; i ++ ) { long x = bits [ i ] ; x = ( ( x >> 1 ) & 0x55555555 L ) | ( ( x & 0x55555555 L ) << 1 ) ; x = ( ( x >> 2 ) & 0x33333333 L ) | ( ( x & 0x33333333 L ) << 2 ) ; x = ( ( x >> 4 ) & 0x0f0f0f0f L ) | ( ( x & 0x0f0f0f0f L ) << 4 ) ; x = ( ( x >> 8 ) & 0x00ff00ff L ) | ( ( x & 0x00ff00ff L ) << 8 ) ; x = ( ( x >> 16 ) & 0x0000ffff L ) | ( ( x & 0x0000ffff L ) << 16 ) ; newBits [ len - i ] = ( int ) x ; } // now correct the int's if the bit size isn't a multiple of 32 if ( size != oldBitsLen * 32 ) { int leftOffset = oldBitsLen * 32 - size ; int currentInt = newBits [ 0 ] >>> leftOffset ; for ( int i = 1 ; i < oldBitsLen ; i ++ ) { int nextInt = newBits [ i ] ; currentInt |= nextInt << ( 32 - leftOffset ) ; newBits [ i - 1 ] = currentInt ; currentInt = nextInt >>> leftOffset ; } newBits [ oldBitsLen - 1 ] = currentInt ; } bits = newBits ; }
Reverses all bits in the array .