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

The following examples show how to use java.math.BigInteger#or() . 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: BigIntegerOrTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Or 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, -1, -2, -2, -91, -2, 31, -1, -11, 125, -22, -83, 30, 95};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.or(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: AddicTranslator.java    From binnavi with Apache License 2.0 6 votes vote down vote up
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction,
    final List<ReilInstruction> instructions) throws InternalTranslationException {
  TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "addic");

  final IOperandTreeNode registerOperand1 =
      instruction.getOperands().get(1).getRootNode().getChildren().get(0);
  final IOperandTreeNode literalOperand1 =
      instruction.getOperands().get(2).getRootNode().getChildren().get(0);

  BigInteger literalValue = BigInteger.valueOf(Long.valueOf(literalOperand1.getValue()));
  if (literalValue.testBit(15)) {
    literalValue = literalValue.or(BigInteger.valueOf(0xFFFF0000L));
  }

  AddGenerator.generate(instruction.getAddress().toLong() * 0x100, environment, instruction,
      instructions, "addic", registerOperand1.getValue(), String.valueOf(literalValue), true,
      false, true, false);
}
 
Example 3
Source File: BigIntegerOrTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Or for two numbers of different signs and the same length
 */
public void testPosNegSameLength() {
    byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
    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, 1, -126, 59, 103, -2, -11, -7, -3, -33, -57, -3, -5, -5, -21};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.or(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: BigIntegerOrTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Or for zero and one
 */
public void testZeroOne() {
    byte aBytes[] = {0};
    byte bBytes[] = {1};
    int aSign = 0;
    int bSign = 1;        
    byte rBytes[] = {1};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.or(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: BigIntegerOrTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Or for two positive numbers of the same length
 */
public void testPosPosSameLength() {
    byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
    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, -2, -3, -4, -4, -1, -66, 95, 47, 123, 59, -13, 39, 30, -97};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.or(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 6
Source File: BigIntegerOrTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Or for two negative numbers; the first is shorter
 */
public void testNegPosFirstShorter() {
    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[] = {-74, 91, 47, -5, -13, -7, -5, -33, -49, -65, -1, -9, -3};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.or(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 7
Source File: BigIntegerOrTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Or for a positive and a negative numbers; the first is longer
 */
public void testPosNegFirstLonger() {
    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[] = {-74, 91, 47, -5, -13, -7, -5, -33, -49, -65, -1, -9, -3};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.or(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 8
Source File: BigIntegerOrTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Or for two positive numbers; the first is shorter
 */
public void testPosPosFirstShorter() {
    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[] = {0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15, -9, 39, 58, -69, 87, 87, -17, -73};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.or(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 9
Source File: TestUtils.java    From pgpverify-maven-plugin with Apache License 2.0 6 votes vote down vote up
public static PGPPublicKey getPGPgpPublicKey(long keyID) {

        BigInteger bigInteger = BigInteger.valueOf(0xffffffffL & keyID);
        BigInteger bigInteger2 = BigInteger.valueOf(keyID);

        bigInteger = bigInteger.shiftLeft(64);
        bigInteger = bigInteger.or(bigInteger2);

        bigInteger = bigInteger.shiftLeft(64);
        bigInteger = bigInteger.or(bigInteger2);

        byte[] bytes = bigInteger.toByteArray();
        if (bytes[0] == 0) {
            // we can remove sign byte
            bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
        }

        PGPPublicKey pgpKey = mock(PGPPublicKey.class);
        when(pgpKey.getFingerprint()).thenReturn(bytes);
        when(pgpKey.isMasterKey()).thenReturn(true);

        return pgpKey;
    }
 
Example 10
Source File: Keccak.java    From aion_api with MIT License 5 votes vote down vote up
private BigInteger rot(BigInteger x, int n) {
    n = n % w;

    BigInteger leftShift = getShiftLeft64(x, n);
    BigInteger rightShift = x.shiftRight(w - n);

    return leftShift.or(rightShift);
}
 
Example 11
Source File: ByteArrayBitStreamReader.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public BigInteger readBigInteger(final int numBits) throws IOException
{
    BigInteger result = BigInteger.ZERO;
    int bitsToRead = numBits;
    if (bitsToRead > 8)
    {
        if (bitOffset != 0)
        {
            final int prefixLength = 8 - bitOffset;
            final long mostSignificantBits = readBits(prefixLength);
            result = BigInteger.valueOf(mostSignificantBits);
            bitsToRead -= prefixLength;
        }

        final int numBytes = bitsToRead / 8;
        final byte[] b = new byte[numBytes];
        readFully(b);
        final BigInteger i = new BigInteger(1, b);
        result = result.shiftLeft(8 * numBytes);
        result = result.or(i);
        bitsToRead &= BYTE_MOD_MASK;
    }
    if (bitsToRead > 0)
    {
        final long value = readBits(bitsToRead);
        result = result.shiftLeft(bitsToRead);
        result = result.or(BigInteger.valueOf(value));
    }
    return result;
}
 
Example 12
Source File: FixedPointType.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
static BigInteger convert(int mBitSize, int nBitSize, BigInteger m, BigInteger n) {
    BigInteger mPadded = m.shiftLeft(nBitSize);
    int nBitLength = n.bitLength();

    // find next multiple of 4
    int shift = (nBitLength + 3) & ~0x03;
    return mPadded.or(n.shiftLeft(nBitSize - shift));
}
 
Example 13
Source File: Prefix6.java    From batfish with Apache License 2.0 5 votes vote down vote up
private static BigInteger getNetworkEnd(BigInteger networkStart, int prefixLength) {
  BigInteger networkEnd = networkStart;
  int onesLength = MAX_PREFIX_LENGTH - prefixLength;
  for (int i = 0; i < onesLength; i++) {
    networkEnd = networkEnd.or(BigInteger.ONE.shiftLeft(i));
  }
  return networkEnd;
}
 
Example 14
Source File: CiscoConversions.java    From batfish with Apache License 2.0 5 votes vote down vote up
private static Route6FilterLine toRoute6FilterLine(ExtendedIpv6AccessListLine fromLine) {
  LineAction action = fromLine.getAction();
  Ip6 ip = fromLine.getSourceIpWildcard().getIp();
  BigInteger minSubnet = fromLine.getDestinationIpWildcard().getIp().asBigInteger();
  BigInteger maxSubnet =
      minSubnet.or(fromLine.getDestinationIpWildcard().getWildcard().asBigInteger());
  int minPrefixLength = fromLine.getDestinationIpWildcard().getIp().numSubnetBits();
  int maxPrefixLength = new Ip6(maxSubnet).numSubnetBits();
  int statedPrefixLength =
      fromLine.getSourceIpWildcard().getWildcard().inverted().numSubnetBits();
  int prefixLength = Math.min(statedPrefixLength, minPrefixLength);
  Prefix6 prefix = new Prefix6(ip, prefixLength);
  return new Route6FilterLine(action, prefix, new SubRange(minPrefixLength, maxPrefixLength));
}
 
Example 15
Source File: FixedPointType.java    From web3j with Apache License 2.0 5 votes vote down vote up
static BigInteger convert(int mBitSize, int nBitSize, BigInteger m, BigInteger n) {
    BigInteger mPadded = m.shiftLeft(nBitSize);
    int nBitLength = n.bitLength();

    // find next multiple of 4
    int shift = (nBitLength + 3) & ~0x03;
    return mPadded.or(n.shiftLeft(nBitSize - shift));
}
 
Example 16
Source File: DecimalStream.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public BigInteger nextBigInteger()
        throws IOException
{
    BigInteger result = BigInteger.ZERO;
    long work = 0;
    int offset = 0;
    long b;
    do {
        b = input.read();
        if (b == -1) {
            throw new OrcCorruptionException("Reading BigInteger past EOF from " + input);
        }
        work |= (0x7f & b) << (offset % 63);
        if (offset >= 126 && (offset != 126 || work > 3)) {
            throw new OrcCorruptionException("Decimal exceeds 128 bits");
        }
        offset += 7;
        // if we've read 63 bits, roll them into the result
        if (offset == 63) {
            result = BigInteger.valueOf(work);
            work = 0;
        }
        else if (offset % 63 == 0) {
            result = result.or(BigInteger.valueOf(work).shiftLeft(offset - 63));
            work = 0;
        }
    }
    while (b >= 0x80);
    if (work != 0) {
        result = result.or(BigInteger.valueOf(work).shiftLeft((offset / 63) * 63));
    }
    // convert back to a signed number
    boolean isNegative = result.testBit(0);
    if (isNegative) {
        result = result.add(BigInteger.ONE);
        result = result.negate();
    }
    result = result.shiftRight(1);
    return result;
}
 
Example 17
Source File: BigIntegerType.java    From tddl with Apache License 2.0 4 votes vote down vote up
@Override
public Object bitOr(Object v1, Object v2) {
    BigInteger i1 = convertFrom(v1);
    BigInteger i2 = convertFrom(v2);
    return i1.or(i2);
}
 
Example 18
Source File: DERObjectIdentifier.java    From BiglyBT with GNU General Public License v2.0 4 votes vote down vote up
DERObjectIdentifier(
    byte[]  bytes)
{
    StringBuilder objId = new StringBuilder();
    long            value = 0;
    BigInteger      bigValue = null;
    boolean         first = true;

    for (int i = 0; i != bytes.length; i++)
    {
        int b = bytes[i] & 0xff;

        if (value < 0x80000000000000L)
        {
            value = value * 128 + (b & 0x7f);
            if ((b & 0x80) == 0)             // end of number reached
            {
                if (first)
                {
                    switch ((int)value / 40)
                    {
                    case 0:
                        objId.append('0');
                        break;
                    case 1:
                        objId.append('1');
                        value -= 40;
                        break;
                    default:
                        objId.append('2');
                        value -= 80;
                    }
                    first = false;
                }

                objId.append('.');
                objId.append(value);
                value = 0;
            }
        }
        else
        {
            if (bigValue == null)
            {
                bigValue = BigInteger.valueOf(value);
            }
            bigValue = bigValue.shiftLeft(7);
            bigValue = bigValue.or(BigInteger.valueOf(b & 0x7f));
            if ((b & 0x80) == 0)
            {
                objId.append('.');
                objId.append(bigValue);
                bigValue = null;
                value = 0;
            }
        }
    }

    this.identifier = objId.toString();
}
 
Example 19
Source File: Composite.java    From OkapiBarcode with Apache License 2.0 4 votes vote down vote up
private void byteprocess(int start, int length) {
    int len = 0;
    int chunkLen = 0;
    BigInteger mantisa;
    BigInteger total;
    BigInteger word;

    /* select the switch for multiple of 6 bytes */
    if ((binary_string.length() % 6) == 0) {
        codeWords[codeWordCount++] = 924;
    } else {
        codeWords[codeWordCount++] = 901;
    }

    while (len < length) {
        chunkLen = length - len;
        if (6 <= chunkLen) /* Take groups of 6 */ {
            chunkLen = 6;
            len += chunkLen;
            total = BigInteger.valueOf(0);

            while ((chunkLen--) != 0) {
                mantisa = BigInteger.valueOf(inputData[start++]);
                total = total.or(mantisa.shiftLeft(chunkLen * 8));
            }

            chunkLen = 5;

            while ((chunkLen--) != 0) {

                word = total.mod(BigInteger.valueOf(900));
                codeWords[codeWordCount + chunkLen] = word.intValue();
                total = total.divide(BigInteger.valueOf(900));
            }
            codeWordCount += 5;
        } else /*  If it remain a group of less than 6 bytes   */ {
            len += chunkLen;
            while ((chunkLen--) != 0) {
                codeWords[codeWordCount++] = inputData[start++];
            }
        }
    }
}
 
Example 20
Source File: BigIntType.java    From es6draft with MIT License 2 votes vote down vote up
/**
 * BigInt::bitwiseOR (x, y)
 * 
 * @param x
 *            the x value
 * @param y
 *            the y value
 * @return the result of bitwise or-ing {@code x} and {@code y}
 */
public static BigInteger bitwiseOR(BigInteger x, BigInteger y) {
    return x.or(y);
}