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

The following examples show how to use java.math.BigInteger#andNot() . 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 File: BigIntegerNotTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * andNot for two positive numbers; the first is longer
 */
public void testAndNotPosPosFirstLonger() {
    byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    int aSign = 1;
    int bSign = 1;        
    byte rBytes[] = {0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -96};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.andNot(bNumber);
    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 2
Source File: BigIntegerNotTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * andNot for two positive numbers; the first is shorter
 */
public void testAndNotPosPosFirstShorter() {
    byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    int aSign = 1;
    int bSign = 1;        
    byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 2};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.andNot(bNumber);
    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 3
Source File: BigIntegerNotTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * andNot for two negative numbers; the first is longer
 */
public void testAndNotNegNegFirstLonger() {
    byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    int aSign = -1;
    int bSign = -1;        
    byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 2};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.andNot(bNumber);
    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
Source File: BigIntegerNotTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * andNot for a negative and a positive numbers; the first is longer
 */
public void testNegPosFirstLonger() {
    byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    int aSign = -1;
    int bSign = 1;        
    byte rBytes[] = {-1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 72};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.andNot(bNumber);
    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 5
Source File: BigIntegerDemoUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() {
    BigInteger i = new BigInteger("17");
    BigInteger j = new BigInteger("7");

    BigInteger and = i.and(j);
    BigInteger or = i.or(j);
    BigInteger not = j.not();
    BigInteger xor = i.xor(j);
    BigInteger andNot = i.andNot(j);
    BigInteger shiftLeft = i.shiftLeft(1);
    BigInteger shiftRight = i.shiftRight(1);

    assertEquals(new BigInteger("1"), and);
    assertEquals(new BigInteger("23"), or);
    assertEquals(new BigInteger("-8"), not);
    assertEquals(new BigInteger("22"), xor);
    assertEquals(new BigInteger("16"), andNot);
    assertEquals(new BigInteger("34"), shiftLeft);
    assertEquals(new BigInteger("8"), shiftRight);
}
 
Example 6
Source File: BinaryFormat.java    From Walrus with GNU General Public License v3.0 5 votes vote down vote up
protected BigInteger applyAtMyPos(BigInteger target, BigInteger value) {
    if (length != null) {
        target = target.andNot(lengthMask().shiftLeft(startPos));
        value = value.and(lengthMask());
    }

    return target.or(value.shiftLeft(startPos));
}
 
Example 7
Source File: Ip6.java    From batfish with Apache License 2.0 4 votes vote down vote up
public Ip6 inverted() {
  BigInteger mask = new BigInteger("+FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
  BigInteger invertedBigInteger = mask.andNot(_ip6);
  return new Ip6(invertedBigInteger);
}
 
Example 8
Source File: BigIntegerMutatorTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
BigInteger apply(BigInteger left, BigInteger right) {
  return left.andNot(right);
}