• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

java.math.BigInteger

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please tell me what is going on in this code snippet :

BigInteger bi = new BigInteger("1023");

// Parse and format to binary
bi = new BigInteger("1111111111", 2); // 1023
String s = bi.toString(2); // 1111111111

The thing which i didn't get is : the purpose of arguments passed in BigInteger("1111111111", 2) constructor.
I mean what are we doing in this code ???
Is it meant to convert Binary numbers into String format ???
and Do we have to supply a Binary number everytime ???
what-if I want a method that convert a Binary number asked by a User from Command-line to Decimal format ??
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rubbal Bhusri wrote:Can anyone please tell me what is going on in this code snippet :

BigInteger bi = new BigInteger("1023");

// Parse and format to binary
bi = new BigInteger("1111111111", 2); // 1023
String s = bi.toString(2); // 1111111111

The thing which i didn't get is : the purpose of arguments passed in BigInteger("1111111111", 2) constructor.
I mean what are we doing in this code ???
Is it meant to convert Binary numbers into String format ???
and Do we have to supply a Binary number everytime ???
what-if I want a method that convert a Binary number asked by a User from Command-line to Decimal format ??



I did not know the whole answer, but a quick trip to the javadocs (http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html) tells me :


public BigInteger(String val,
int radix)
Translates the String representation of a BigInteger in the specified radix into a BigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more digits in the specified radix. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).
Parameters:
val - String representation of BigInteger.
radix - radix to be used in interpreting val.



and


public String toString(int radix)
Returns the String representation of this BigInteger in the given radix.



in this case, radix means the base, so using 2 here means base 2 or binary. so all you are doing is giving a biginteger a String ("111111") along with an integer (2) that tells BigInteger that this string is a number represented as base 2 and then converting it back into binary with the toString(2)
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>The thing which i didn't get is : the purpose of arguments passed in BigInteger("1111111111", 2) constructor.

The ability to read (and if need be, to locate) the API documentation is a valuable asset for a programmer.

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How a number is written or displayed is independent of what the value is. "five" is the same as "5" is the same as "V" is the same as "0101"

Now, you have to tell someone what schema you are using. I would have to tell you the above are "english words", "decimal digit", "roman numerals" and "binary string".

BigInteger works the same way. you can pass in values, but you have to tell java whether it is binary, hexidecimal, or something else.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:How a number is written or displayed is independent of what the value is. "five" is the same as "5" is the same as "V" is the same as "0101"

Now, you have to tell someone what schema you are using. I would have to tell you the above are "english words", "decimal digit", "roman numerals" and "binary string".

BigInteger works the same way. you can pass in values, but you have to tell java whether it is binary, hexidecimal, or something else.



Do you mean to say , whatever we pass the values in arguments e.g. ("1111111111", 2) Its just going to convert that argument into String format e.g. String s = bi.toString(2); because when I try to print the value of 's' in System.out.println(s); command, it just printed the value as it is, which was 1111111111.

Seeing this , I thought that this BigInteger(String val, int radix) constructor is useless.
Can you please , show me some working,practical, live example of this method ???
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rubbal Bhusri wrote:Can you please , show me some working,practical, live example of this method ???


I don't know how practical it is, but instead of your example, suppose you had a binary string like this:
"10101010101010010100101001010010100101010101010010100101011111111001010010111111010001111110100011010"

How would you propose to convert that to a number? It's too big for any other numeric data type except BigDecimal, and it's not a decimal; it's an integer.

Winston
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rubbal Bhusri wrote:Seeing this , I thought that this BigInteger(String val, int radix) constructor is useless.
Can you please , show me some working,practical, live example of this method ???


I don't quite understand where you're coming from, but you seem to be surprised that if you create a BigInteger out of a binary string, and then convert it back to a binary string, you get the same answer as you started with. Is that really the problem? What else would you expect?
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give any e.g. of working-application, where this method has been used ???
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this code working, where I used the method intValue() of class BigInteger to convert the String passed as argument to the BigInteger("0000000011", 2) constructor to an int type. And it worked very well.
import java.math.*;
class Conversion {
public static void main(String[] args){
// Parse and format to binary
BigInteger bi = new BigInteger("0000000011", 2);
String s = bi.toString(2);
int i = bi.intValue();
System.out.println("The converted integer is :" +

i);
System.out.println();
System.out.println(s);

}

}

BUT BUT BUT

I would still like to see an e.g. of working-application where this BigInteger class has been used .......//
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this:

outputs this:

The integer is :127

1111111
127
7f



You can put any value you want into the BigInteger. You can do it using any base. Each of these puts the exact same value into the BigInteger

BigInteger bi = new BigInteger("1111111", 2);
BigInteger bi = new BigInteger("127");
BigInteger bi = new BigInteger("7f", 16);

once the value is in there, you can take it out in any format you want - base2, base 10, base 16...whatever.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rubbal Bhusri wrote:I got this code working, where I used the method intValue() of class BigInteger to convert the String passed as argument to the BigInteger("0000000011", 2) constructor to an int type. And it worked very well.


And you could have done it just as easily (and faster) with Integer.parseInt("0000000011", 2); and furthermore, it will throw an exception if the string cannot be converted to an int (ie, it is too large).

BigInteger is designed for holding arbitrary integers (ie, an integer value of virtually any size). If you know that the value is supposed to be an int, then use an int (or an Integer).

Winston
 
expectation is the root of all heartache - shakespeare. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic