5.7 Pairwise Swap

Using mask1 = 101010 to find odd bits, mask2 = 010101 to find even bits. Then mask1 >>> 1 to even pos, and mask2 << 1 to odd pos. The swap res will be then or result.

    public static int swaPair(int n){
        return (((n & 0xaaaaaaaa) >>> 1) | ((n & 0x55555555) << 1));
    }

你可能感兴趣的:(BitMani)