BigInteger bit and,or,xor,test,flip,negate
In this chapter you will learn:
- How to calculate Big andNot on BigInteger
- How to get the number of bit in a BigInteger
- How to clear designated bit from BigInteger
- How to flip designated bit in BigInteger
- How to negate a BigInteger
- How to do not operation on BigInteger
- How to do or operation on a BigInteger
- How to set bit value on BigInteger
- If the designated bit is set
- How to do bit xor calculation on BigInteger
Big andNot on BigInteger
BigInteger andNot(BigInteger val)
Returns a BigInteger whose value is (this & ~val).
import java.math.BigInteger;
//from jav a 2 s .com
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
bi = bi.andNot(bi);
}
}
The output:
Get the number of bit in a BigInteger
int bitLength()
Returns the number of bits in the minimal two's-complement
representation of this BigInteger, excluding a sign bit.
import java.math.BigInteger;
/*from ja v a2s . c o m*/
public class Main {
public static void main(String args[]) {
BigInteger n = new BigInteger("1000000000000");
BigInteger one = new BigInteger("1");
while (!n.isProbablePrime(7))
n = n.add(one);
System.out.println(n.toString(10) + " is probably prime.");
System.out.println("It is " + n.bitLength() + " bits in length.");
}
}
The output:
Clear designated bit from BigInteger
BigInteger clearBit(int n)
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.
import java.math.BigInteger;
//from jav a 2 s . c o m
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
bi = bi.clearBit(3);
}
}
The output:
Flip designated bit in BigInteger
BigInteger flipBit(int n)
returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.
import java.math.BigInteger;
//from ja v a2 s.c o m
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
bi = bi.flipBit(3);
System.out.println(bi);
}
}
The output:
Negate a BigInteger
BigInteger negate()
returns a BigInteger whose value is (-this).
import java.math.BigInteger;
/*from jav a 2 s .com*/
public class Main {
public static void main(String[] argv) throws Exception {
BigInteger bi1 = new BigInteger("1234567890123456890");
bi1 = bi1.negate();
System.out.println(bi1);
}
}
The output:
Not operation on BigInteger
BigInteger not()
returns a BigInteger whose value is (~this).
import java.math.BigInteger;
//from ja va2 s. c o m
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
bi = bi.not();
}
}
or a BigInteger
BigInteger or(BigInteger val)
returns a BigInteger whose value is (this | val).
import java.math.BigInteger;
/*j a v a 2s . c om*/
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
bi = bi.or(bi);
}
}
Set bit value on BigInteger
BigInteger setBit(int n)
returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.
import java.math.BigInteger;
//from ja va 2 s.c om
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
bi = bi.setBit(3);
System.out.println(bi);
}
}
The output:
If the designated bit is set
boolean testBit(int n)
Returns true if and only if the designated bit is set.
import java.math.BigInteger;
// ja v a 2s .c o m
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
boolean b = bi.testBit(3);
b = bi.testBit(16);
System.out.println(b);
}
}
The output:
Bit xor calculation on BigInteger
BigInteger xor(BigInteger val)
Returns a BigInteger whose value is (this ^ val).
import java.math.BigInteger;
/* j a va2 s. c o m*/
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
BigInteger bi = new BigInteger(bytes);
bi = bi.xor(bi);
System.out.println(bi);
}
}
The output:
Next chapter...
What you will learn in the next chapter: