BigInteger bit and,or,xor,test,flip,negate

In this chapter you will learn:

  1. How to calculate Big andNot on BigInteger
  2. How to get the number of bit in a BigInteger
  3. How to clear designated bit from BigInteger
  4. How to flip designated bit in BigInteger
  5. How to negate a BigInteger
  6. How to do not operation on BigInteger
  7. How to do or operation on a BigInteger
  8. How to set bit value on BigInteger
  9. If the designated bit is set
  10. 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:

  1. How to left shift bit value on BigInteger
  2. How to right shift bit on BigInteger
Home » Java Tutorial » BigDecimal BigInteger

BigDecimal

    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format

BigInteger

    BigInteger class
    BigInteger creation
    BigInteger add, subtract, multiply and divide
    BigInteger power and modPow
    BigInteger conversion
    BigInteger to String
    BigInteger bit and,or,xor,test,flip,negate
    BigInteger bit shift left and right
    BigInteger prime value
    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format