Negation
by Isai Damier, Android Engineer @ Google

/***************************************************************************
 * Author: Isai Damier
 * Title: Negation
 * Project: geekviewpoint
 * Package: algorithms
 *
 * Statement:
 *   Given a set of bits, x, find its negation.
 *
 * Sample Input:  0101100
 * Sample Output: 1010011
 *
 * Technical Details:
 *   The negation of a set x is the set comprising all the elements in the
 *   universe that are not in x. For example if the context is all the
 *   ciphers in the decimal system and s1 = [0,2,5,8] then the negation of
 *   s1 is [1,3,4,6,7,9]. For a set of bits the negation is simply the
 *   bitwise NOT of the set. For example if x = 0110101 then the
 *   negation of x is 1001010.
 *
 * Alternate Algorithm:
 *   The negation of a set of bits x may also be calculated as ALL_BITS ^ x,
 *   where ^ is the bitwise XOR operator. For example, if x = 0110101 then
 *   perforce ALL_BITS = 1111111. Hence, negative x = 1111111^0110101=1001010
 *
 * JAVA Note:
 *   Java does not support unsigned numbers. Therefore, there are times when
 *   ~x must be replaced with ALL_BITS^x in order to work properly. The
 *   programmer must define ALL_BITS. If x is an integer, for example, then
 *   ALL_BITS is a string of sixteen ones.
 **************************************************************************/ 
 public int negation(int x) {
  int u = Integer.parseInt(""1111111111111111"", 2);
  return u^x;// or return ~x
}
import org.junit.Test;
import static org.junit.Assert.*;

public class BitwiseTest {

 /**
   * Test of negation method, of class Bitwise.
   */
  @Test
  public void testNegation() {
    System.out.println(""negation"");
    int x = Integer.parseInt(""1111111111111010"", 2);
    int r = Integer.parseInt(""0000000000000101"", 2);
    int u = Integer.parseInt(""1111111111111111"", 2);
    Bitwise bits = new Bitwise();
    assertEquals(r, bits.negation(x));
    assertEquals(x, bits.negation(r));
    assertEquals(u ^ x, r);
  }
}