วันศุกร์ที่ 14 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.add(BigInteger val)

Description Of BigInteger: add(BigInteger val)

The java.math.BigInteger.add(BigInteger val) returns a BigInteger object whose value is (this + val).

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("456");
  BigInteger bi2 = new BigInteger("789");
  System.out.println(bi1.add(bi2)); // output : 1245 (456 + 789)
  System.out.println(bi1); // output : 456
  bi1 = bi1.add(bi2);
  System.out.println(bi1); // output : 1245
  System.out.println(bi1.add(BigInteger.valueOf(1))); // output : 1246
 }
}


yengo หรือ buzzcity

วันพฤหัสบดีที่ 13 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.abs()

Description Of BigInteger: abs()

The java.math.BigInteger.abs() returns a BigInteger whose value is the absolute value of this BigInteger

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("456");
  BigInteger bi2 = new BigInteger("-456");
  System.out.println("Absolute value of " + bi1 + " is " + bi1.abs()); // Absolute value of 456 is 456
  System.out.println("Absolute value of " + bi2 + " is " + bi2.abs()); // Absolute value of -456 is 456
 }
}


yengo หรือ buzzcity

วันพุธที่ 12 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.and(BigInteger val)

Description Of BigInteger: and(BigInteger val)

The java.math.BigInteger.and(BigInteger val) returns a BigInteger whose value is (this & val). This method returns a negative BigInteger if and only if this and val are both negative.

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("6"); // 110
  BigInteger bi2 = new BigInteger("3"); // 011
  System.out.println(bi1.and(bi2)); // 010
  
  bi1 = new BigInteger("5"); // 0101
  bi2 = new BigInteger("8"); // 1000
  System.out.println(bi1.and(bi2)); // 0000
 }
}


yengo หรือ buzzcity

วันอังคารที่ 11 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.andNot(BigInteger val)

Description Of BigInteger: andNot(BigInteger val)

The java.math.BigInteger.andNot(BigInteger val) returns a BigInteger whose value is (this & ~val). This method, which is equivalent to and(val.not()), is provided as a convenience for masking operations. This method returns a negative BigInteger if and only if this is negative and val is positive

Code Example Java BigInteger

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("6"); // 110
  BigInteger bi2 = new BigInteger("3"); // 011 ~010=101
  System.out.println(bi1.andNot(bi2)); // 100
  
  bi1 = new BigInteger("5"); // 0101
  bi2 = new BigInteger("8"); // 1000 ~1000=0111
  System.out.println(bi1.andNot(bi2)); // 0101
 }
}


yengo หรือ buzzcity

วันจันทร์ที่ 10 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.clearBit(int n)

Description Of BigInteger: clearBit(int n)

The java.math.BigInteger.clearBit(int n) The java.math.BigInteger.clearBit(int n) returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. It computes (this & ~(1<<n)).

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("7"); 
  System.out.println(bi1.clearBit(2)); // 3
  // 7 = 111, 111 << 2 = 11100, ~11100 = 00011, 111 & 00011 = 00011 = 3
  
  int testbit = 7; // 111 , 7 << 1 = 14 = 1110, 7 << 2 = 28 = 11100
  System.out.println(testbit << 2); // 28
 }
}


yengo หรือ buzzcity

วันอาทิตย์ที่ 9 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.divide(BigInteger val)

Description Of BigInteger: divide(BigInteger val)

The java.math.BigInteger.divide(BigInteger val) returns a BigInteger whose value is (this / val).

Code Example Java BigInteger

import java.math.BigInteger;

public class CodeFromDoesystem {
 public static void main(String[] args) throws Exception {
  BigInteger bi1 = new BigInteger("4");
  BigInteger bi2 = new BigInteger("2");
  System.out.println(bi1.divide(bi2)); // output : 2 (4 / 2)
  System.out.println(bi2.divide(bi1)); // output : 0 (2 / 4)
  System.out.println(bi1); // output : 4
  System.out.println(bi1.divide(BigInteger.valueOf(4))); // output : 1
 }
}


yengo หรือ buzzcity

วันเสาร์ที่ 8 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.compareTo(BigInteger val)

Description Of BigInteger:compareTo(BigInteger val)

The java.math.BigInteger.compareTo(BigInteger val) compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=).

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("7");
  BigInteger bi2 = new BigInteger("4");

  // create int object
  int res;

  // compare bi1 with bi2
  res = bi1.compareTo(bi2);

  if (res == 0) {
   System.out.println("Both values are equal");
  } else if (res == 1) {
   System.out.println("First Value is greater");
  } else if (res == -1) {
   System.out.println("Second value is greater");
  }
 }
}


yengo หรือ buzzcity

วันศุกร์ที่ 7 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.divideAndRemainder(BigInteger val)

Description Of BigInteger: divideAndRemainder(BigInteger val)

The java.math.BigInteger.divideAndRemainder(BigInteger val) returns an array of two BigIntegers containing (this / val) followed by (this % val).

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("7");
  BigInteger bi2 = new BigInteger("4");

  BigInteger bi[] = bi1.divideAndRemainder(bi2);

  System.out.println("length Of return " + bi.length);

  System.out.println("Quotient is " + bi[0]); // 7/4
  System.out.println("Remainder is " + bi[1]); // 7%4
 }
}


yengo หรือ buzzcity

วันพฤหัสบดีที่ 6 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.doubleValue()

Description Of BigInteger: doubleValue()

The java.math.BigInteger.doubleValue() converts this BigInteger to a double. This conversion is similar to the narrowing primitive conversion from double to float.

If this BigInteger has too great a magnitude to represent as a double, it will be converted to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY as appropriate. Even when the return value is finite, this conversion can lose information about the precision of the BigInteger value.

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("123");
  BigInteger bi2 = new BigInteger("123456789123456789");

  double bi1Double = bi1.doubleValue();
  double bi2Double = bi2.doubleValue();

  String str1 = "Double value of " + bi1 + " is " + bi1Double;
  String str2 = "Double value of " + bi2 + " is " + bi2Double;

  // print d1, d2 values
  System.out.println(str1);
  System.out.println(str2);
 }
}


yengo หรือ buzzcity

วันพุธที่ 5 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.equals(Object x)

Description Of BigInteger: equals(Object x)

The java.math.BigInteger.equals(Object x) compares this BigInteger with the specified Object for equality

BigInteger.equals(Object x) method returns true if and only if the specified Object is a BigInteger whose value is numerically equal to this BigInteger

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("123");
  BigInteger bi2 = new BigInteger("123");

  // compare bi1 with bi2
  Boolean b1 = bi1.equals(bi2);

  // compare bi1 with an object value 123, which is not a BigIntger
  Boolean b2 = bi1.equals("123");

  System.out.println(b1); // output true
  System.out.println(b2); // output false
 }
}


yengo หรือ buzzcity

วันอังคารที่ 4 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.flipBit(int n)

Description Of BigInteger: flipBit(int n)

The java.math.BigInteger.flipBit(int n) returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped. It computes (this ^ (1<<n))

BigInteger.flipBit(int n) method returns this ^ (1<<n)

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  // assign value to bi1
  BigInteger bi1 = new BigInteger("8"); // 1000

  // perform flipbit operation on bi1 with index 1
  BigInteger bi2 = bi1.flipBit(1);

  System.out.println(bi2); // output 10
 }
}


yengo หรือ buzzcity

วันจันทร์ที่ 3 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.floatValue()

Description Of BigInteger: floatValue()

The java.math.BigInteger.floatValue() converts this BigInteger to a float

BigInteger.floatValue() method returns this BigInteger converted to a float

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  // assign values to bi1, bi2
  BigInteger bi1 = new BigInteger("123");
  BigInteger bi2 = new BigInteger("-123");

  // assign float values of bi1, bi2 to f1, f2
  Float f1 = bi1.floatValue();
  Float f2 = bi2.floatValue();

  // print f1, f2 values
  System.out.println(f1); // 123.0
  System.out.println(f2); // -123.0
 }
}


yengo หรือ buzzcity

วันอาทิตย์ที่ 2 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.gcd(BigInteger val)

Description Of BigInteger: gcd(BigInteger val)

The java.math.BigInteger.gcd(BigInteger val) returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). It returns 0 if this==0 && val==0.

BigInteger.floatValue() This method returns a BigInteger whose value is GCD(abs(this), abs(val))

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  // assign values to bi1, bi2
  BigInteger bi1 = new BigInteger("24");
  BigInteger bi2 = new BigInteger("32");

  // assign gcd of bi1, bi2 to bi3
  BigInteger bi3 = bi1.gcd(bi2);

  System.out.println(bi3); // output 8
 }
}


yengo หรือ buzzcity

วันเสาร์ที่ 1 พฤศจิกายน พ.ศ. 2557

java.math.BigInteger.getLowestSetBit()

Description Of BigInteger: getLowestSetBit()

The java.math.BigInteger.getLowestSetBit() returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit). It returns -1 if this BigInteger contains no one bits. It computes (this==0? -1 : log2(this & -this))

BigInteger.getLowestSetBit() This method returns the index of the rightmost one bit in this BigInteger

Code Example Java BigInteger

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("8");// 1000
     BigInteger bi2 = new BigInteger("7");// 0111
     BigInteger bi3 = new BigInteger("6");// 0110

     // perform getLowestSetBit on bi1, bi2
     int i1 = bi1.getLowestSetBit();
     int i2 = bi2.getLowestSetBit();
     int i3 = bi3.getLowestSetBit();

     System.out.println(i1); // output 3
     System.out.println(i2); // output 0
     System.out.println(i3); // output 1
 }
}


yengo หรือ buzzcity