Java Code Examples for java.math.BigInteger#flipBit()

The following examples show how to use java.math.BigInteger#flipBit() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: tutorials   File: BigIntegerDemoUnitTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() {
    BigInteger i = new BigInteger("1018");

    int bitCount = i.bitCount();
    int bitLength = i.bitLength();
    int getLowestSetBit = i.getLowestSetBit();
    boolean testBit3 = i.testBit(3);
    BigInteger setBit12 = i.setBit(12);
    BigInteger flipBit0 = i.flipBit(0);
    BigInteger clearBit3 = i.clearBit(3);

    assertEquals(8, bitCount);
    assertEquals(10, bitLength);
    assertEquals(1, getLowestSetBit);
    assertEquals(true, testBit3);
    assertEquals(new BigInteger("5114"), setBit12);
    assertEquals(new BigInteger("1019"), flipBit0);
    assertEquals(new BigInteger("1010"), clearBit3);
}
 
Example 2
/**
 * flipBit(int n) of a negative n
 */
public void testFlipBitException() {
    byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = -7;
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    try {
        aNumber.flipBit(number);
        fail("ArithmeticException has not been caught");
    } catch (ArithmeticException e) {
    }
}
 
Example 3
/**
 * flipBit(int n) zero
 */
public void testFlipBitZero() {
    byte aBytes[] = {0};
    int aSign = 0;
    int number = 0;
    byte rBytes[] = {1};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 4
/**
 * flipBit(int n) outside zero
 */
public void testFlipBitZeroOutside1() {
    byte aBytes[] = {0};
    int aSign = 0;
    int number = 62;
    byte rBytes[] = {64, 0, 0, 0, 0, 0, 0, 0};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue("incorrect value", resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 5
/**
 * flipBit(int n) outside zero
 */
public void testFlipBitZeroOutside2() {
    byte aBytes[] = {0};
    int aSign = 0;
    int number = 63;
    byte rBytes[] = {0, -128, 0, 0, 0, 0, 0, 0, 0};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue("incorrect value", resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 6
/**
 * flipBit(int n) the leftmost bit in a negative number
 */
public void testFlipBitLeftmostNegative() {
    byte aBytes[] = {1, -128, 56, 100, -15, 35, 26};
    int aSign = -1;
    int number = 48;
    byte rBytes[] = {-1, 127, -57, -101, 14, -36, -26, 49};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", -1, result.signum());
}
 
Example 7
/**
 * flipBit(int n) the leftmost bit in a positive number
 */
public void testFlipBitLeftmostPositive() {
    byte aBytes[] = {1, -128, 56, 100, -15, 35, 26};
    int aSign = 1;
    int number = 48;
    byte rBytes[] = {0, -128, 56, 100, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 8
/**
 * flipBit(int n) inside a negative number
 */
public void testFlipBitNegativeInside1() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = -1;
    int number = 15;
    byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, 92, -26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", -1, result.signum());
}
 
Example 9
/**
 * flipBit(int n) inside a negative number
 */
public void testFlipBitNegativeInside2() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = -1;
    int number = 45;
    byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -14, -92, -4, 14, -36, -26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", -1, result.signum());
}
 
Example 10
/**
 * flipBit(int n) inside a negative number with all ones in bit representation 
 */
public void testFlipBitNegativeInside3() {
    String as = "-18446744073709551615";
    String res = "-18446744073709551611";
    int number = 2;
    BigInteger aNumber = new BigInteger(as);
    BigInteger result = aNumber.flipBit(number);
    assertEquals(res, result.toString());
}
 
Example 11
/**
 * flipBit(0) in the negative number of length 1
 * with all ones in bit representation.
 * the resulting number's length is 2.
 */
public void testFlipBitNegativeInside4() {
    String as = "-4294967295";
    String res = "-4294967296";
    int number = 0;
    BigInteger aNumber = new BigInteger(as);
    BigInteger result = aNumber.flipBit(number);
    assertEquals(res, result.toString());
}
 
Example 12
/**
 * flipBit(0) in the negative number of length 2
 * with all ones in bit representation.
 * the resulting number's length is 3.
 */
public void testFlipBitNegativeInside5() {
    String as = "-18446744073709551615";
    String res = "-18446744073709551616";
    int number = 0;
    BigInteger aNumber = new BigInteger(as);
    BigInteger result = aNumber.flipBit(number);
    assertEquals(res, result.toString());
}
 
Example 13
/**
 * flipBit(int n) outside a negative number
 */
public void testFlipBitNegativeOutside1() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = -1;
    int number = 150;
    byte rBytes[] = {-65, -1, -1, -1, -1, -1, -2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", -1, result.signum());
}
 
Example 14
/**
 * flipBit(int n) outside a negative number
 */
public void testFlipBitNegativeOutside2() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = -1;
    int number = 191;
    byte rBytes[] = {-1, 127, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", -1, result.signum());
}
 
Example 15
/**
 * flipBit(int n) inside a positive number
 */
public void testFlipBitPositiveInside1() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 15;
    byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, -93, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 16
/**
 * flipBit(int n) inside a positive number
 */
public void testFlipBitPositiveInside2() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 45;
    byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 13, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 17
/**
 * flipBit(int n) outside a positive number
 */
public void testFlipBitPositiveOutside1() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 150;
    byte rBytes[] = {64, 0, 0, 0, 0, 0, 1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 18
/**
 * flipBit(int n) outside a positive number
 */
public void testFlipBitPositiveOutside2() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 191;
    byte rBytes[] = {0, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.flipBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 19
Source Project: AVM   File: BigIntegerTarget.java    License: MIT License 4 votes vote down vote up
@Callable
public static BigInteger flipBit(BigInteger testValue, int num) {
    return testValue.flipBit(num);
}
 
Example 20
Source Project: pitest   File: BigIntegerMutatorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
BigInteger apply(BigInteger left, BigInteger right) {
  return left.flipBit(right.intValue());
}