Parse Decimal String to Create BigInteger in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:47:22

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

Parse Hexadecimal String to Create BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:46:43

2K+ Views

To parse the hexadecimal string to create BigInteger, use the following BigInteger constructor and set the radix as 16 for Hexadecimal.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 16 −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       String hexStr = "290f98";       one = new BigInteger("250");       // parsing       two = ... Read More

Parsing and Formatting a BigInteger into Binary in Java

Samual Sam
Updated on 29-Jun-2020 05:45:49

2K+ Views

Firstly, take two BigInteger objects and set values.BigInteger one, two; one = new BigInteger("99"); two = new BigInteger("978");Now, parse BigInteger object “two” into Binary.two = new BigInteger("1111010010", 2); String str = two.toString(2);Above, we have used the following constructor. Here, radix is set as 2 for Binary for both BigInteger constructor and toString() method.BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;     ... Read More

Parsing and Formatting a BigInteger into Octal in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:44:57

321 Views

Firstly, take two BigInteger objects and set values.BigInteger one, two; one = new BigInteger("99");Now, parse BigInteger object “two” into Octal.two = new BigInteger("1100", 8); String str = two.toString(8);Above, we have used the following constructor. Here, radix is set as 8 for Octal. for both BigInteger constructor and toString() method.BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new ... Read More

Parsing and Formatting a Byte Array into Binary in Java

Samual Sam
Updated on 29-Jun-2020 05:44:09

4K+ Views

Set a BigInteger object.BigInteger one;Now, create a ByteArray.byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 }; one = new BigInteger(byteArr);For Binary, we have used 2 as the toString() method parameter.String strResult = one.toString(2);The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       // BigInteger object       BigInteger one;       byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };       one = new BigInteger(byteArr);       String strResult = one.toString(2);       System.out.println("ByteArray to Binary = "+strResult);    } }OutputByteArray to Binary = 10000000000000000

Parsing and Formatting a Byte Array into Octal in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:43:13

705 Views

Set a BigInteger object.BigInteger one;Now, create a ByteArray.byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 }; one = new BigInteger(byteArr);For Octal, we have used 8 as the toString() method parameter.String strResult = one.toString(8);The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       // ByteArray       byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };       one = new BigInteger(byteArr);       // Octal       String strResult = one.toString(8);       System.out.println("ByteArray to Octal = "+strResult);    } }OutputByteArray to Octal = 200000

Perform XOR Operation on BigInteger in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:41:49

229 Views

The java.math.BigInteger.xor(BigInteger val) returns a BigInteger whose value is (this ^ val). This method returns a negative BigInteger if and only if exactly one of this and val are negative. Here, “val” is the value to be XOR'ed with this BigInteger.Firstly, create two BigInteger objects −one = new BigInteger("6"); two = new BigInteger("-5");Now, perform XOR −three = one.xor(two);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("6");       two = new BigInteger("-5");       ... Read More

Perform Operations on BigInteger in Java

Sharon Christine
Updated on 29-Jun-2020 05:40:35

143 Views

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. Here, “val” is the value to be AND'ed with this BigInteger.First, create BigInteger objects −BigInteger one, two, three; one = new BigInteger("12"); two = new BigInteger("6");Perform AND operation on first and second object −three = one.and(two);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 ... Read More

Implement NOT Operation on BigInteger in Java

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

144 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 −Example Live Demoimport 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 −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2, ... Read More

Implement OR Operation on BigInteger in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:37:00

162 Views

TheBigInteger.or(BigInteger val) returns a BigInteger whose value is (this | val). This method returns a negative BigInteger if and only if either this or val is negative.Here, “val” is the value to be OR'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("6");       two = one.or(one);       System.out.println("Result (or operation): " +two);    } }OutputResult (or operation): 6Let us see another example −Example Live Demoimport java.math.*; public class Demo { ... Read More

Advertisements