Subtraction
by Isai Damier, Android Engineer @ Google

/***************************************************************************
 * Author: Isai Damier
 * Title: Subtraction
 * Project: geekviewpoint
 * Package: algorithms
 *
 * Statement:
 *   Given two sets of bits, x and y, find their difference.
 *
 * Sample Input:  0010010010
 * Sample Output: 0010010011
 *
 * Technical Details: The subtraction of two sets is the subset comprising
 *   the elements that appear in the minuend x and not in the subtrahend y.
 *   For example if s1 = [a,b,f,h,k] and s2 = [b,c,d,f,h,i] then the
 *   subtraction of s1 and s2 is [a,k]. For two sets of bits the subtraction
 *   is a two steps process. First, the negative of the second set is
 *   calculated. Then the bitwise AND of the result and the first set is
 *   taken. For example if x = 0110101 and y=0101100 then the subtraction of
 *   x and y follows:
 *    step_1: z = negative y = ~y = 1010011.
 *    step_2: x AND z = x & z = x & (~y) = 0010001
 *
 **************************************************************************/ 
 public int subtraction(int x, int y) {
  return x & ~y;
}
import org.junit.Test;
import static org.junit.Assert.*;

public class BitwiseTest {

 /**
   * Test of subtraction method, of class Bitwise.
   */
  @Test
  public void testSubtraction() {
    System.out.println(""subtraction"");
    Bitwise bits = new Bitwise();
    int x = Integer.parseInt(""0101100"", 2);
    int y = Integer.parseInt(""0110101"", 2);
    int u = Integer.parseInt(""0001000"", 2);
    assertEquals(u, bits.subtraction(x, y));

    x = Integer.parseInt(""0101100"", 2);
    y = Integer.parseInt(""0110101"", 2);
    u = Integer.parseInt(""0010001"", 2);
    assertEquals(u, bits.subtraction(y, x));
  }
}