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

The following examples show how to use java.math.BigInteger#setBit() . 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
private BigInteger getMaskedAddress(boolean one) {
    BigInteger numAddress = netAddress;

    int numBits;
    if (isV4) {
        numBits = 32 - networkMask;
    } else {
        numBits = 128 - networkMask;
    }

    for (int i = 0; i < numBits; i++) {
        if (one)
            numAddress = numAddress.setBit(i);
        else
            numAddress = numAddress.clearBit(i);
    }
    return numAddress;
}
 
Example 2
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 3
private BigInteger getMaskedAddress(boolean one) {
    BigInteger numAddress = netAddress;

    int numBits;
    if (isV4) {
        numBits = 32 - networkMask;
    } else {
        numBits = 128 - networkMask;
    }

    for (int i = 0; i < numBits; i++) {
        if (one)
            numAddress = numAddress.setBit(i);
        else
            numAddress = numAddress.clearBit(i);
    }
    return numAddress;
}
 
Example 4
Source Project: admin-plus   File: RightsHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 利用BigInteger对权限进行2的权的和计算
 * @param rights int型权限编码数组
 * @return 2的权的和
 */
public static BigInteger sumRights(int[] rights){
	BigInteger num = new BigInteger("0");
	for(int i=0; i<rights.length; i++){
		num = num.setBit(rights[i]);
	}
	return num;
}
 
Example 5
/**
 * setBit(int n) inside a positive number
 */
public void testSetBitPositiveInside3() {
    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, 45, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.setBit(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 6
/**
 * setBit(int n) the leftmost bit in a negative number
 */
public void testSetBitLeftmostNegative() {
    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.setBit(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
/**
 * setBit(int n) inside a positive number
 */
public void testSetBitPositiveInside4 () {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 50;
    byte rBytes[] = {1, -128, 56, 100, -2, -76, 93, 45, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.setBit(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
/**
 * setBit(int n) outside a negative number
 */
public void testSetBitNegativeOutside2() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = -1;
    int number = 191;
    byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.setBit(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
private static void testValueOfMulPow52(long value, int p5, int p2) throws Exception {
    BigInteger bi = BigInteger.valueOf(value & ~LONG_SIGN_MASK);
    if (value < 0) {
        bi = bi.setBit(63);
    }
    check(biPow52(p5, p2).multiply(bi), FDBigInteger.valueOfMulPow52(value, p5, p2),
            "valueOfMulPow52(" + Long.toHexString(value) + "." + p5 + "," + p2 + ")");
}
 
Example 10
Source Project: ethdroid   File: Keccak.java    License: MIT License 5 votes vote down vote up
private BigInteger getShiftLeft64(BigInteger value, int shift) {
    BigInteger retValue = value.shiftLeft(shift);
    BigInteger tmpValue = value.shiftLeft(shift);

    if (retValue.compareTo(BIT_64) > 0) {
        for (int i = 64; i < 64 + shift; i++) {
            tmpValue = tmpValue.clearBit(i);
        }

        tmpValue = tmpValue.setBit(64 + shift);
        retValue = tmpValue.and(retValue);
    }

    return retValue;
}
 
Example 11
Source Project: securify   File: Sar.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void computeResultValues() {
    if (getInput()[0].hasConstantValue() && getInput()[1].hasConstantValue() &&
            getOutput()[0].getConstantValue() == Variable.VALUE_UNDEFINED) {
    	BigInteger r;
        BigInteger a = BigIntUtil.fromUint256(getInput()[0].getConstantValue());
        BigInteger b = BigIntUtil.fromInt256(getInput()[1].getConstantValue());
        if(a.compareTo(BigInteger.valueOf(256)) >= 0) {
        	if(b.signum() == -1) {
        		r = BigInteger.valueOf(-1);
        	}else {
        		r = BigInteger.ZERO;
        	}
        } else if(b.signum() != -1) {
            r = b.shiftRight(a.intValueExact());
        } else  {
        	r = b;
        	for (int i = 0; i < a.intValueExact(); i++) {
        		r.shiftRight(1);
        		r.setBit(256);
}
        }
        getOutput()[0].setConstantValue(BigIntUtil.toInt256(r));
    } else {
        getOutput()[0].setConstantValue(Variable.VALUE_ANY);
    }
}
 
Example 12
/**
 * Returns the value of this {@code UnsignedLong} as a {@link BigInteger}.
 */
public BigInteger bigIntegerValue() {
  BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
  if (value < 0) {
    bigInt = bigInt.setBit(Long.SIZE - 1);
  }
  return bigInt;
}
 
Example 13
/**
 * Returns the value of this {@code UnsignedLong} as a {@link BigInteger}.
 */


public BigInteger bigIntegerValue() {
  BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
  if (value < 0) {
    bigInt = bigInt.setBit(Long.SIZE - 1);
  }
  return bigInt;
}
 
Example 14
private static void testValueOfMulPow52(long value, int p5, int p2) throws Exception {
    BigInteger bi = BigInteger.valueOf(value & ~LONG_SIGN_MASK);
    if (value < 0) {
        bi = bi.setBit(63);
    }
    check(biPow52(p5, p2).multiply(bi), FDBigInteger.valueOfMulPow52(value, p5, p2),
            "valueOfMulPow52(" + Long.toHexString(value) + "." + p5 + "," + p2 + ")");
}
 
Example 15
/**
 * setBit(int n) of a negative n
 */
public void testSetBitException() {
    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.setBit(number);
        fail("ArithmeticException has not been caught");
    } catch (ArithmeticException e) {
    }
}
 
Example 16
private static void testValueOfMulPow52(long value, int p5, int p2) throws Exception {
    BigInteger bi = BigInteger.valueOf(value & ~LONG_SIGN_MASK);
    if (value < 0) {
        bi = bi.setBit(63);
    }
    check(biPow52(p5, p2).multiply(bi), FDBigInteger.valueOfMulPow52(value, p5, p2),
            "valueOfMulPow52(" + Long.toHexString(value) + "." + p5 + "," + p2 + ")");
}
 
Example 17
private static void testValueOfMulPow52(long value, int p5, int p2) throws Exception {
    BigInteger bi = BigInteger.valueOf(value & ~LONG_SIGN_MASK);
    if (value < 0) {
        bi = bi.setBit(63);
    }
    check(biPow52(p5, p2).multiply(bi), FDBigInteger.valueOfMulPow52(value, p5, p2),
            "valueOfMulPow52(" + Long.toHexString(value) + "." + p5 + "," + p2 + ")");
}
 
Example 18
private static void testValueOfMulPow52(long value, int p5, int p2) throws Exception {
    BigInteger bi = BigInteger.valueOf(value & ~LONG_SIGN_MASK);
    if (value < 0) {
        bi = bi.setBit(63);
    }
    check(biPow52(p5, p2).multiply(bi), FDBigInteger.valueOfMulPow52(value, p5, p2),
            "valueOfMulPow52(" + Long.toHexString(value) + "." + p5 + "," + p2 + ")");
}
 
Example 19
/**
 * setBit(int n) the leftmost bit in a positive number
 */
public void testSetBitTopPositive() {
    byte aBytes[] = {1, -128, 56, 100, -15, 35, 26};
    int aSign = 1;
    int number = 63;
    byte rBytes[] = {0, -128, 1, -128, 56, 100, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.setBit(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 20
Source Project: AVM   File: BigIntegerTarget.java    License: MIT License 4 votes vote down vote up
@Callable
public static byte[] setBit(byte[] val, int num) {
    BigInteger testValue = new BigInteger(val);
    BigInteger result = testValue.setBit(num);
    return result.toByteArray();
}