Here you can find the source of flipBits(int n)
public static int flipBits(int n)
//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); } }