Example #1
0
  public BDD varRange(BigInteger lo, BigInteger hi) {
    if (lo.signum() < 0 || hi.compareTo(size()) >= 0 || lo.compareTo(hi) > 0) {
      throw new BDDException("range <" + lo + ", " + hi + "> is invalid");
    }

    BDDFactory factory = getFactory();
    BDD result = factory.zero();
    int[] ivar = this.vars();
    while (lo.compareTo(hi) <= 0) {
      BDD v = factory.universe();
      for (int n = ivar.length - 1; ; n--) {
        if (lo.testBit(n)) {
          v.andWith(factory.ithVar(ivar[n]));
        } else {
          v.andWith(factory.nithVar(ivar[n]));
        }
        BigInteger mask = BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE);
        if (!lo.testBit(n) && lo.or(mask).compareTo(hi) <= 0) {
          lo = lo.or(mask).add(BigInteger.ONE);
          break;
        }
      }
      result.orWith(v);
    }
    return result;
  }
 /** @tests java.math.BigInteger#or(java.math.BigInteger) */
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "or",
     args = {java.math.BigInteger.class})
 public void test_orLjava_math_BigInteger() {
   for (BigInteger[] element : booleanPairs) {
     BigInteger i1 = element[0], i2 = element[1];
     BigInteger res = i1.or(i2);
     assertTrue("symmetry of or", res.equals(i2.or(i1)));
     int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
     for (int i = 0; i < len; i++) {
       assertTrue("or", (i1.testBit(i) || i2.testBit(i)) == res.testBit(i));
     }
   }
 }
Example #3
0
  /*
   * Get a random or boundary-case number. This is designed to provide
   * a lot of numbers that will find failure points, such as max sized
   * numbers, empty BigIntegers, etc.
   *
   * If order is less than 2, order is changed to 2.
   */
  private static BigInteger fetchNumber(int order) {
    boolean negative = rnd.nextBoolean();
    int numType = rnd.nextInt(7);
    BigInteger result = null;
    if (order < 2) order = 2;

    switch (numType) {
      case 0: // Empty
        result = BigInteger.ZERO;
        break;

      case 1: // One
        result = BigInteger.ONE;
        break;

      case 2: // All bits set in number
        int numBytes = (order + 7) / 8;
        byte[] fullBits = new byte[numBytes];
        for (int i = 0; i < numBytes; i++) fullBits[i] = (byte) 0xff;
        int excessBits = 8 * numBytes - order;
        fullBits[0] &= (1 << (8 - excessBits)) - 1;
        result = new BigInteger(1, fullBits);
        break;

      case 3: // One bit in number
        result = BigInteger.ONE.shiftLeft(rnd.nextInt(order));
        break;

      case 4: // Random bit density
        int iterations = rnd.nextInt(order - 1);
        result = BigInteger.ONE.shiftLeft(rnd.nextInt(order));
        for (int i = 0; i < iterations; i++) {
          BigInteger temp = BigInteger.ONE.shiftLeft(rnd.nextInt(order));
          result = result.or(temp);
        }
        break;
      case 5: // Runs of consecutive ones and zeros
        result = ZERO;
        int remaining = order;
        int bit = rnd.nextInt(2);
        while (remaining > 0) {
          int runLength = Math.min(remaining, rnd.nextInt(order));
          result = result.shiftLeft(runLength);
          if (bit > 0) result = result.add(ONE.shiftLeft(runLength).subtract(ONE));
          remaining -= runLength;
          bit = 1 - bit;
        }
        break;

      default: // random bits
        result = new BigInteger(order, rnd);
    }

    if (negative) result = result.negate();

    return result;
  }
Example #4
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   while (in.hasNextBigInteger()) {
     BigInteger a = in.nextBigInteger();
     BigInteger b = in.nextBigInteger();
     BigInteger n = null;
     n = a.or(b);
     System.out.println(n);
   }
 }
  DERObjectIdentifier(byte[] bytes) {
    StringBuffer objId = new StringBuffer();
    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 #6
0
 @Override
 public Pointer apply(SmartList<Pointer> args) throws MintException {
   Pointer arg0 = args.get(0);
   Pointer arg1 = args.get(1);
   if (arg0.type == Constants.BIG_INT_TYPE && arg1.type == Constants.BIG_INT_TYPE) {
     BigInteger op0 = PointerTools.dereferenceBigInt(arg0);
     BigInteger op1 = PointerTools.dereferenceBigInt(arg1);
     return Heap.allocateBigInt(op0.or(op1));
   }
   Integer operand0 = PointerTools.dereferenceInt(arg0);
   Integer operand1 = PointerTools.dereferenceInt(arg1);
   if (operand0 == null || operand1 == null) {
     throw new MintException("Bitwise or can only be applied to " + "integers.");
   }
   return Heap.allocateInt(operand0 | operand1);
 }
Example #7
0
  public static void bitwise(int order) {

    /* Test identity x^y == x|y &~ x&y */
    int failCount = 0;
    for (int i = 0; i < size; i++) {
      BigInteger x = fetchNumber(order);
      BigInteger y = fetchNumber(order);
      BigInteger z = x.xor(y);
      BigInteger w = x.or(y).andNot(x.and(y));
      if (!z.equals(w)) failCount++;
    }
    report("Logic (^ | & ~)", failCount);

    /* Test identity x &~ y == ~(~x | y) */
    failCount = 0;
    for (int i = 0; i < size; i++) {
      BigInteger x = fetchNumber(order);
      BigInteger y = fetchNumber(order);
      BigInteger z = x.andNot(y);
      BigInteger w = x.not().or(y).not();
      if (!z.equals(w)) failCount++;
    }
    report("Logic (&~ | ~)", failCount);
  }
Example #8
0
 public static BigInteger LOr(BigInteger x, BigInteger y) {
   return x.or(y);
 }
  public int countWays(int[] platformMount, int[] platformLength, int[] balls) {

    // count the maximum area that can be covered by platforms

    int max_left = 10000;
    int max_right = -10000;

    for (int i = 0; i < platformMount.length; i++) {
      if (platformMount[i] - platformLength[i] < max_left) {
        max_left = platformMount[i] - platformLength[i];
      }

      if (platformMount[i] + platformLength[i] > max_right) {
        max_right = platformMount[i] + platformLength[i];
      }
    }

    //		System.out.println(max_left);
    //		System.out.println(max_right);

    int[] b = new int[balls.length + 2];
    b[0] = max_left;
    System.arraycopy(balls, 0, b, 1, balls.length);
    b[balls.length + 1] = max_right;

    int start = max_left;

    int cnt[] = new int[platformMount.length];
    Arrays.fill(cnt, 0);

    for (int i = 0; i < platformMount.length; i++) {

      for (int j = 0; j < b.length; j++) {
        int end = b[j];
        int d = end - start;

        if (platformLength[i] < d) {

          int l =
              (start > platformMount[i] - platformLength[i])
                  ? start
                  : platformMount[i] - platformLength[i];

          int count = end - (l + platformLength[i]);
          if (count < 0) {
            count = -count;
          }
          cnt[i] = cnt[i] + count;

          // System.out.println(i+ ") " + count + " positions");

          //					 System.out.println("["+start+";"+end+"] and ["+l);
          //				 System.out.println(cnt[i]);
        }
        start = end;
      }
    }

    BigInteger res = BigInteger.valueOf(cnt[0]);

    for (int i = 1; i < cnt.length; i++) {
      res = res.multiply(BigInteger.valueOf(cnt[i]));
    }

    res.or(BigInteger.valueOf(1000000009));
    return res.intValue();
  }
Example #10
0
  public static void serialize() throws Exception {
    int failCount = 0;

    String bitPatterns[] = {
      "ffffffff00000000ffffffff00000000ffffffff00000000",
      "ffffffffffffffffffffffff000000000000000000000000",
      "ffffffff0000000000000000000000000000000000000000",
      "10000000ffffffffffffffffffffffffffffffffffffffff",
      "100000000000000000000000000000000000000000000000",
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
      "-ffffffff00000000ffffffff00000000ffffffff00000000",
      "-ffffffffffffffffffffffff000000000000000000000000",
      "-ffffffff0000000000000000000000000000000000000000",
      "-10000000ffffffffffffffffffffffffffffffffffffffff",
      "-100000000000000000000000000000000000000000000000",
      "-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    };

    for (int i = 0; i < bitPatterns.length; i++) {
      BigInteger b1 = new BigInteger(bitPatterns[i], 16);
      BigInteger b2 = null;

      File f = new File("serialtest");

      try (FileOutputStream fos = new FileOutputStream(f)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
          oos.writeObject(b1);
          oos.flush();
        }

        try (FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis)) {
          b2 = (BigInteger) ois.readObject();
        }

        if (!b1.equals(b2) || !b1.equals(b1.or(b2))) {
          failCount++;
          System.err.println("Serialized failed for hex " + b1.toString(16));
        }
      }
      f.delete();
    }

    for (int i = 0; i < 10; i++) {
      BigInteger b1 = fetchNumber(rnd.nextInt(100));
      BigInteger b2 = null;
      File f = new File("serialtest");
      try (FileOutputStream fos = new FileOutputStream(f)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
          oos.writeObject(b1);
          oos.flush();
        }

        try (FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis)) {
          b2 = (BigInteger) ois.readObject();
        }
      }

      if (!b1.equals(b2) || !b1.equals(b1.or(b2))) failCount++;
      f.delete();
    }

    report("Serialize", failCount);
  }