Java Bit Flip flipBits(int n)

Here you can find the source of flipBits(int n)

Description

flip Bits

License

Open Source License

Declaration

public static int flipBits(int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int flipBits(int n) {
        System.out.println("n = " + Integer.toBinaryString(n));
        int mask = 0;
        for (int i = 0; i < 31; i++) {
            mask <<= 1;// w w  w  .  jav  a  2 s . c o m
            if (i % 2 == 0)
                mask |= 1;
        }
        System.out.println("mask = " + Integer.toBinaryString(mask));
        int evens = n & mask;
        System.out.println("evens = " + Integer.toBinaryString(evens));
        int odds = n & ~mask;
        System.out.println("odds = " + Integer.toBinaryString(odds));
        odds >>>= 1;
        evens <<= 1;
        System.out.println("evens shifted = " + Integer.toBinaryString(evens));
        System.out.println("odds shifted  = " + Integer.toBinaryString(odds));
        return (odds | evens);
    }
}

Related

  1. flip(int value)
  2. flip(long a)
  3. flipBitAsBinaryString(int flipBit)
  4. flipBitAt(int bitIndex, byte b)
  5. flipBits(byte[] bytes, int start, int bitLength)
  6. flipBits(int value)
  7. flipC(long v, int off)
  8. FLIPENDIAN_INT32(int x)