Object Oriented Programming Articles

Page 278 of 589

Create BigInteger via long type variable in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 616 Views

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Firstly, set a long value −Long l = 198L;Now, create a new object for BigInteger and pass the above value −BigInteger bInteger = new BigInteger(l);The following is an example −Exampleimport java.math.BigInteger; public class Demo {    public static void main(String[] argv) throws Exception {       Long l = 198L;       BigInteger bInteger = BigInteger.valueOf(l);       System.out.println(bInteger);    } }Output198

Read More

Create BigInteger from byte array in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

BigInteger class provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Let’s say the following is our byte array −byte[] arr = new byte[] { 0x1, 0x00, 0x00 };We will now convert them to BigInteger −BigInteger bInteger = new BigInteger(arr);The following is an example that creates BigInteger from a byte array in Java.Exampleimport java.math.BigInteger; public class Demo {    public static void main(String[] argv) throws Exception {       byte[] arr = new byte[] { 0x1, 0x00, 0x00 };       BigInteger bInteger = new BigInteger(arr);       System.out.println(bInteger);    } }Output65536

Read More

Negate a BigInteger in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 267 Views

Use the BigInteger negate() method in Java to negate a BigInteger.First, let us create an object −BigInteger one, two; one = new BigInteger("200");Negate the above and assign it to the second object −two = one.negate();The following is an example −Exampleimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("200");       System.out.println("Actual Value: " +one);       // negate       two = one.negate();       System.out.println("Negated Value: " +two);    } }OutputActual Value: 200 Negated Value: -200

Read More

Performing Bitwise Operations with BigInteger in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 404 Views

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Let us work with the testBit() method in Java to perform Bitwise operation. The java.math.BigInteger.testBit(int n) returns true if and only if the designated bit is set −The following is an example −Exampleimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one;       Boolean two;       one = new BigInteger("5");   ...

Read More

Shift right in a BigInteger in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 247 Views

To shift right in a BigInteger, use the shiftRight() method.The java.math.BigInteger.shiftRight(int n) returns a BigInteger whose value is (this >> n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. It computes floor(this / 2n).The following is an example −Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       one = new BigInteger("25");       one = one.shiftRight(3);       System.out.println("Result: " +one);    } }OutputResult: 3

Read More

Java Program to implement NOT operation on BigInteger

Samual Sam
Samual Sam
Updated on 11-Mar-2026 182 Views

The BigInteger.not() method returns a BigInteger whose value is (~this). This method returns a negative value if and only if this BigInteger is non-negative.The following is an example −Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("6");       two = one.not();       System.out.println("Result (not operation): " +two);    } }OutputResult (not operation): -7Let us see another example −Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2, bi3, bi4; ...

Read More

Left pad an integer in Java with zeros

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 3K+ Views

Let us see an example first to understand how an integer looks with left padding −888 //left padding with spaces 0000000999 //left padding with 7 zerosLet us see an example to left pad a number with zero −Examplepublic class Demo {    public static void main(String[] args) {       int val = 9899;       System.out.println(String.format("%05d",val));    } }Output09899Let us see another example that pads a greater number of zeros −Examplepublic class Demo {    public static void main(String[] args) {       int val = 9899;       System.out.println(String.format("%010d", val));    } }Output0000009899

Read More

Make the first letter caps and the rest lowercase in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

The following is an example −Examplepublic class Demo {    public static void main(String[] args) {       String str = "laptop";       System.out.println("Original String = " +str);       // letter one       String strOne = str.substring(0,1).toUpperCase();       // remaining letters       String strTwo = str.substring(1).toLowerCase();       System.out.println("Resultant String = "+strOne + strTwo);    } }OutputOriginal String = laptop Resultant String = Laptop

Read More

Parse Octal string to create BigInteger in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 230 Views

To parse the octal string to create BigInteger, use the following BigInteger constructor and set the radix as 8 for Octal −BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.In the below example, we have set the BigInteger and radix is set as 8.BigInteger one, two; one = new BigInteger("12"); two = new BigInteger("4373427", 8);The following is the complete example −Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("12");   ...

Read More

Parse decimal string to create BigInteger in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 347 Views

To parse the decimal string to create BigInteger, just set the decimal string in the BigInteger.Here is our BigInteger.BigInteger one; String decStr = "687879"; one = new BigInteger(decStr);Let us see another example −Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       String decStr = "4373427";       one = new BigInteger("250");       two = new BigInteger(decStr);       System.out.println("Result (BigInteger) : " +one);       System.out.println("Result (Parsing Decimal String) : " +two);    } }OutputResult (BigInteger) : 250 Result (Parsing Decimal String) : 4373427

Read More
Showing 2771–2780 of 5,881 articles
« Prev 1 276 277 278 279 280 589 Next »
Advertisements