Clear Bit
by Isai Damier, Android Engineer @ Google

/***************************************************************************
 * Author: Isai Damier
 * Title: Clear Bit
 * Project: geekviewpoint
 * Package: algorithms
 *
 * Statement:
 *   Given a bit sequence x, clear the kth bit; where the zeroth (0th) bit
 *   is the least significant (rightmost) bit.
 *
 * Sample Input: integer representation of bit sequence 0010010010
 * Sample Output: 0010010000 after clearing the 1st bit
 *
 * Technical Details:
 *   Let x = 0010010010. Then only the 1st, 4th, and 7th bits are set.
 *   Therefore, clearBit(x,1) would return 0010010000; whereas
 *   clearBit(x,3) would have no effect and so would return 0010010010.
 *
 *   clearBit is a three steps operation:
 *   step_1] Using the shift-left operator <<, create a bit sequence
 *     where only the kth bit is set.
 *   step_2] Take the negation of the sequence in step_1. step_2 effectively
 *     creates a sequence where all bits are set except the kth bit.
 *   step_3] Take the bitwise AND of x and the sequence in step_2. In digital
 *     logic a&1 is equal to 1 whereas a&0 is equal to 0. Therefore, this
 *     operation force clear the kth bit while leaving the other bits
 *     unchanged.
 *
 **************************************************************************/ 
 public int clearBit(int x, int kth) {
  return (x & ~(1 << kth));
}
import org.junit.Test;
import static org.junit.Assert.*;

public class BitwiseTest {

  /**
   * Test of clearBit method, of class Bitwise.
   */
  @Test
  public void testClearBit() {
    System.out.println(""clearBit"");
    Bitwise bits = new Bitwise();
    int x = Integer.parseInt(""0010010010"", 2);
    int a = Integer.parseInt(""0010010000"", 2);
    int b = Integer.parseInt(""0010000010"", 2);
    int c = Integer.parseInt(""0000010010"", 2);

    assertEquals(x, bits.clearBit(x, 0));
    assertEquals(a, bits.clearBit(x, 1));
    assertEquals(b, bits.clearBit(x, 4));
    assertEquals(c, bits.clearBit(x, 7));
  }
}