gt:         int nBytes = (numBits + 7) / 8;
context: /*
   *  This code originates from the bouncy castle library and didn't have a copyright header
   */
  
  package com.codename1.util;
  
  import java.util.Random;
  import java.util.Stack;
  
  /**
   * A simplified version of big integer from the bouncy castle implementation
   */
  public class BigInteger
  {
      // The primes b/w 2 and ~2^10
      /*
              3   5   7   11  13  17  19  23  29
          31  37  41  43  47  53  59  61  67  71
          73  79  83  89  97  101 103 107 109 113
          127 131 137 139 149 151 157 163 167 173
          179 181 191 193 197 199 211 223 227 229
          233 239 241 251 257 263 269 271 277 281
          283 293 307 311 313 317 331 337 347 349
          353 359 367 373 379 383 389 397 401 409
          419 421 431 433 439 443 449 457 461 463
          467 479 487 491 499 503 509 521 523 541
          547 557 563 569 571 577 587 593 599 601
          607 613 617 619 631 641 643 647 653 659
          661 673 677 683 691 701 709 719 727 733
          739 743 751 757 761 769 773 787 797 809
          811 821 823 827 829 839 853 857 859 863
          877 881 883 887 907 911 919 929 937 941
          947 953 967 971 977 983 991 997
          1009 1013 1019 1021 1031
      */
  
      // Each list has a product < 2^31
      private static final int[][] primeLists = new int[][]
      {
          new int[]{ 3, 5, 7, 11, 13, 17, 19, 23 },
          new int[]{ 29, 31, 37, 41, 43 },
          new int[]{ 47, 53, 59, 61, 67 },
          new int[]{ 71, 73, 79, 83 },
          new int[]{ 89, 97, 101, 103 },
  
          new int[]{ 107, 109, 113, 127 },
          new int[]{ 131, 137, 139, 149 },
          new int[]{ 151, 157, 163, 167 },
          new int[]{ 173, 179, 181, 191 },
          new int[]{ 193, 197, 199, 211 },
  
          new int[]{ 223, 227, 229 },
          new int[]{ 233, 239, 241 },
          new int[]{ 251, 257, 263 },
          new int[]{ 269, 271, 277 },
          new int[]{ 281, 283, 293 },
  
          new int[]{ 307, 311, 313 },
          new int[]{ 317, 331, 337 },
          new int[]{ 347, 349, 353 },
          new int[]{ 359, 367, 373 },
          new int[]{ 379, 383, 389 },
  
          new int[]{ 397, 401, 409 },
          new int[]{ 419, 421, 431 },
          new int[]{ 433, 439, 443 },
          new int[]{ 449, 457, 461 },
          new int[]{ 463, 467, 479 },
  
          new int[]{ 487, 491, 499 },
          new int[]{ 503, 509, 521 },
          new int[]{ 523, 541, 547 },
          new int[]{ 557, 563, 569 },
          new int[]{ 571, 577, 587 },
  
          new int[]{ 593, 599, 601 },
          new int[]{ 607, 613, 617 },
          new int[]{ 619, 631, 641 },
          new int[]{ 643, 647, 653 },
          new int[]{ 659, 661, 673 },
  
          new int[]{ 677, 683, 691 },
          new int[]{ 701, 709, 719 },
          new int[]{ 727, 733, 739 },
          new int[]{ 743, 751, 757 },
          new int[]{ 761, 769, 773 },
  
          new int[]{ 787, 797, 809 },
          new int[]{ 811, 821, 823 },
          new int[]{ 827, 829, 839 },
          new int[]{ 853, 857, 859 },
          new int[]{ 863, 877, 881 },
  
          new int[]{ 883, 887, 907 },
          new int[]{ 911, 919, 929 },
          new int[]{ 937, 941, 947 },
          new int[]{ 953, 967, 971 },
          new int[]{ 977, 983, 991 },
  
          new int[]{ 997, 1009, 1013 },
          new int[]{ 1019, 1021, 1031 },
      };
  
      private static int[] primeProducts;
  
      private static final long IMASK = 0xffffffffL;
  
      private static final int[] ZERO_MAGNITUDE = new int[0];
  
      public static final BigInteger ZERO = new BigInteger(0, ZERO_MAGNITUDE);
      public static final BigInteger ONE = valueOf(1);
      private static final BigInteger TWO = valueOf(2);
      private static final BigInteger THREE = valueOf(3);
  
      static
      {
          ZERO.nBits = 0; ZERO.nBitLength = 0;
          ONE.nBits = 1;  ONE.nBitLength = 1;
          TWO.nBits = 1;  TWO.nBitLength = 2;
  
          primeProducts = new int[primeLists.length];
  
          for (int i = 0; i < primeLists.length; ++i)
          {
              int[] primeList = primeLists[i];
              int product = 1;
              for (int j = 0; j < primeList.length; ++j)
              {
                  product *= primeList[j];
              }
              primeProducts[i] = product;
          }
      }
      
      private int sign; // -1 means -ve; +1 means +ve; 0 means 0;
      private int[] magnitude; // array of ints with [0] being the most significant
      private int nBits = -1; // cache bitCount() value
      private int nBitLength = -1; // cache bitLength() value
      private long mQuote = -1L; // -m^(-1) mod b, b = 2^32 (see Montgomery mult.)
      
      private BigInteger()
      {
      }
  
      private BigInteger(int signum, int[] mag)
      {
          if (mag.length > 0)
          {
              sign = signum;
  
              int i = 0;
              while (i < mag.length && mag[i] == 0)
              {
                  i++;
              }
              if (i == 0)
              {
                  magnitude = mag;
              }
              else
              {
                  // strip leading 0 bytes
                  int[] newMag = new int[mag.length - i];
                  System.arraycopy(mag, i, newMag, 0, newMag.length);
                  magnitude = newMag;
                  if (newMag.length == 0)
                      sign = 0;
              }
          }
          else
          {
              magnitude = mag;
              sign = 0;
          }
      }
  
      public BigInteger(String sval) throws NumberFormatException
      {
          this(sval, 10);
      }
  
      public BigInteger(String sval, int rdx) throws NumberFormatException
      {
          if (sval.length() == 0)
          {
              throw new NumberFormatException("Zero length BigInteger");
          }
  
          if (rdx < Character.MIN_RADIX || rdx > Character.MAX_RADIX)
          {
              throw new NumberFormatException("Radix out of range");
          }
  
          int index = 0;
          sign = 1;
  
          if (sval.charAt(0) == '-')
          {
              if (sval.length() == 1)
              {
                  throw new NumberFormatException("Zero length BigInteger");
              }
  
              sign = -1;
              index = 1;
          }
  
          // strip leading zeros from the string value
          while (index < sval.length() && Character.digit(sval.charAt(index), rdx) == 0)
          {
              index++;
          }
  
          if (index >= sval.length())
          {
              // zero value - we're done
              sign = 0;
              magnitude = new int[0];
              return;
          }
  
          //////
          // could we work out the max number of ints required to store
          // sval.length digits in the given base, then allocate that
          // storage in one hit?, then generate the magnitude in one hit too?
          //////
  
          BigInteger b = ZERO;
          BigInteger r = valueOf(rdx);
          while (index < sval.length())
          {
              // (optimise this by taking chunks of digits instead?)
              b = b.multiply(r).add(valueOf(Character.digit(sval.charAt(index), rdx)));
              index++;
          }
  
          magnitude = b.magnitude;
          return;
      }
  
      public BigInteger(byte[] bval) throws NumberFormatException
      {
          if (bval.length == 0)
          {
              throw new NumberFormatException("Zero length BigInteger");
          }
  
          sign = 1;
          if (bval[0] < 0)
          {
              sign = -1;
          }
          magnitude = makeMagnitude(bval, sign);
          if (magnitude.length == 0) {
              sign = 0;
          }
      }
  
      /**
       * If sign >= 0, packs bytes into an array of ints, most significant first
       * If sign <  0, packs 2's complement of bytes into 
       * an array of ints, most significant first,
       * adding an extra most significant byte in case bval = {0x80, 0x00, ..., 0x00}
       *
       * @param bval
       * @param sign
       * @return
       */
      private int[] makeMagnitude(byte[] bval, int sign)
      {
          if (sign >= 0) {
              int i;
              int[] mag;
              int firstSignificant;
  
              // strip leading zeros
              for (firstSignificant = 0; firstSignificant < bval.length
                      && bval[firstSignificant] == 0; firstSignificant++);
  
              if (firstSignificant >= bval.length)
              {
                  return new int[0];
              }
  
              int nInts = (bval.length - firstSignificant + 3) / 4;
              int bCount = (bval.length - firstSignificant) % 4;            
              if (bCount == 0)
                  bCount = 4;
              // n = k * (n / k) + n % k
              // bval.length - firstSignificant + 3 = 4 * nInts + bCount - 1
              // bval.length - firstSignificant + 4 - bCount = 4 * nInts
  
              mag = new int[nInts];
              int v = 0;
              int magnitudeIndex = 0;
              for (i = firstSignificant; i < bval.length; i++)
              {
                  // bval.length + 4 - bCount - i + 4 * magnitudeIndex = 4 * nInts
                  // 1 <= bCount <= 4
                  v <<= 8;
                  v |= bval[i] & 0xff;
                  bCount--;
                  if (bCount <= 0)
                  {
                      mag[magnitudeIndex] = v;
                      magnitudeIndex++;
                      bCount = 4;
                      v = 0;
                  }
              }
              // 4 - bCount + 4 * magnitudeIndex = 4 * nInts
              // bCount = 4 * (1 + magnitudeIndex - nInts)
              // 1 <= bCount <= 4
              // So bCount = 4 and magnitudeIndex = nInts = mag.length
  
  //            if (magnitudeIndex < mag.length)
  //            {
  //                mag[magnitudeIndex] = v;
  //            }
              return mag;
          }
          else {
              int i;
              int[] mag;
              int firstSignificant;
              
  
              // strip leading -1's
              for (firstSignificant = 0; firstSignificant < bval.length - 1
                      && bval[firstSignificant] == 0xff; firstSignificant++);
  
              int nBytes = bval.length;
              boolean leadingByte = false;
  
              // check for -2^(n-1)
              if (bval[firstSignificant] == 0x80) {
                  for (i = firstSignificant + 1; i < bval.length; i++) {
                      if (bval[i] != 0) {
                          break;
                      }
                  }
                  if (i == bval.length) {
                      nBytes++;
                      leadingByte = true;
                  }
              }
  
              int nInts = (nBytes - firstSignificant + 3) / 4;
              int bCount = (nBytes - firstSignificant) % 4;
              if (bCount == 0)
                  bCount = 4;
  
              // n = k * (n / k) + n % k
              // nBytes - firstSignificant + 3 = 4 * nInts + bCount - 1
              // nBytes - firstSignificant + 4 - bCount = 4 * nInts
              // 1 <= bCount <= 4
  
              mag = new int[nInts];
              int v = 0;
              int magnitudeIndex = 0;
              // nBytes + 4 - bCount - i + 4 * magnitudeIndex = 4 * nInts
              // 1 <= bCount <= 4
              if (leadingByte) {
                  // bval.length + 1 + 4 - bCount - i + 4 * magnitudeIndex = 4 * nInts
                  bCount--;
                  // bval.length + 1 + 4 - (bCount + 1) - i + 4 * magnitudeIndex = 4 * nInts
                  // bval.length + 4 - bCount - i + 4 * magnitudeIndex = 4 * nInts
                  if (bCount <= 0)
                  {
                      magnitudeIndex++;
                      bCount = 4;
                  }
                  // bval.length + 4 - bCount - i + 4 * magnitudeIndex = 4 * nInts
                  // 1 <= bCount <= 4
              }
              for (i = firstSignificant; i < bval.length; i++)
              {
                  // bval.length + 4 - bCount - i + 4 * magnitudeIndex = 4 * nInts
                  // 1 <= bCount <= 4
                  v <<= 8;
                  v |= ~bval[i] & 0xff;
                  bCount--;
                  if (bCount <= 0)
                  {
                      mag[magnitudeIndex] = v;
                      magnitudeIndex++;
                      bCount = 4;
                      v = 0;
                  }
              }
              // 4 - bCount + 4 * magnitudeIndex = 4 * nInts
              // 1 <= bCount <= 4
              // bCount = 4 * (1 + magnitudeIndex - nInts)
              // 1 <= bCount <= 4
              // So bCount = 4 and magnitudeIndex = nInts = mag.length
  
  //            if (magnitudeIndex < mag.length)
  //            {
  //                mag[magnitudeIndex] = v;
  //            }
              mag = inc(mag);
  
              // TODO Fix above so that this is not necessary?
              if (mag[0] == 0)
              {
                  int[] tmp = new int[mag.length - 1];
                  System.arraycopy(mag, 1, tmp, 0, tmp.length);
                  mag = tmp;
              }
  
              return mag;
          }
      }
      
      
  
      public BigInteger(int sign, byte[] mag) throws NumberFormatException
      {
          if (sign < -1 || sign > 1)
          {
              throw new NumberFormatException("Invalid sign value");
          }
  
          if (sign == 0)
          {
              this.sign = 0;
              this.magnitude = new int[0];
              return;
          }
  
          // copy bytes
          this.magnitude = makeMagnitude(mag, 1);
          this.sign = sign;
      }
  
      public BigInteger(int numBits, Random rnd) throws IllegalArgumentException
      {
          if (numBits < 0)
          {
              throw new IllegalArgumentException("numBits must be non-negative");
          }
  
          this.nBits = -1;
          this.nBitLength = -1;
  
          if (numBits == 0)
          {
  //          this.sign = 0;
              this.magnitude = ZERO_MAGNITUDE;
              return;
          }
  
  