Implement AND-NOT Operation on BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:35:51

136 Views

TheBigInteger.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. Here, “val” is the value to be complemented and AND'ed with this BigInteger.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("12");       two = new BigInteger("6");       three = ... Read More

Left Pad a String with a Specified Character in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:34:29

280 Views

The following is our string −String str = "Amit";Now take a StringBuilder object −StringBuilder strBuilder = new StringBuilder();Set the character you want to pad with a string −Character ch = '^';Perform left padding and extend the string length to 5 (one for the additional character). The character that will be padded comes on the left −while (strBuilder.length() + str.length() < 5) { strBuilder.append(ch); }The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Amit";       Character ch = '^';       StringBuilder strBuilder = ... Read More

Left Pad a String with Spaces in Java

Samual Sam
Updated on 29-Jun-2020 05:33:33

330 Views

Let us see an example first to understand how a string looks with left padding −demotext //left padding with spaces 0000000demotext //left padding with 7 zerosThe following is our string −String str = "Jack";Now take a StringBuilder object −StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length. The spaces that will be padded comes on the left. Append the spaces here −while (strBuilder.length() + str.length() < 10) {    strBuilder.append(' '); }The following is an example to pad a string to the left with spacesExample Live Demopublic class Demo {    public static void main(String[] args) {   ... Read More

Left Pad an Integer in Java with Zeros

karthikeya Boyini
Updated on 29-Jun-2020 05:32:26

2K+ 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 −Example Live Demopublic 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 −Example Live Demopublic class Demo {    public static void main(String[] args) {       int val = 9899;       System.out.println(String.format("%010d", val));    } }Output0000009899

Left Pad a String in Java with Zeros

Samual Sam
Updated on 29-Jun-2020 05:30:58

585 Views

The following is our string −String str = "Tim";Now take a StringBuilder object −StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length. We have set it till 20, that would include the current string as well. The zeros that will be padded comes on the left. Append the zeros here −while (strBuilder.length() + str.length() < 10) { strBuilder.append('0'); }The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Tim";       StringBuilder strBuilder = new StringBuilder();       while (strBuilder.length() + str.length() ... Read More

Make the First Letter Caps and the Rest Lowercase in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:30:03

1K+ Views

The following is an example −Example Live Demopublic 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

Create BigInteger via Long Type Variable in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:29:11

564 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 −Example Live Demoimport 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

Create BigInteger from Byte Array in Java

Samual Sam
Updated on 29-Jun-2020 05:28:41

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.Example Live Demoimport 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

Working with BigInteger Values in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:27:19

188 Views

The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.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.The following is an example that displays how we can work with BigInteger values.Example Live Demoimport java.math.BigInteger; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger bi1, bi2, bi3;       // assign values to bi1, bi2   ... Read More

Multiply One BigInteger to Another BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:26:40

470 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.To multiply one BigInteger to another, use the BigInteger multiply() method.First, let us create some objects −BigInteger one, two, three; one = new BigInteger("2"); two = new BigInteger("8");Multiply the above and assign it to the third object −three = one.multiply(two);The following is an example −Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, ... Read More

Advertisements