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

The following examples show how to use java.math.BigInteger#setBit() . 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: NetworkSpace.java    From SimpleOpenVpn-Android with Apache License 2.0 6 votes vote down vote up
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 File: NetworkSpace.java    From bitmask_android with GNU General Public License v3.0 6 votes vote down vote up
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 3
Source File: BigIntegerDemoUnitTest.java    From tutorials with 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 4
Source File: UnsignedLong.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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 5
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 6
Source File: TestFDBigInteger.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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 7
Source File: TestFDBigInteger.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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 8
Source File: TestFDBigInteger.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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 9
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source File: TestFDBigInteger.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 11
Source File: RightsHelper.java    From admin-plus with 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 12
Source File: UnsignedLong.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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
Source File: Sar.java    From securify with 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 14
Source File: Keccak.java    From ethdroid with 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 15
Source File: TestFDBigInteger.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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 16
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 17
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 18
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 19
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 20
Source File: BigIntegerTarget.java    From AVM with 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();
}