java - How to convert large integer number to binary? -
sorry possible duplicate post, saw many similar topics here none needed. before posting question want explicitly state question not homework.
so question is: how convert large integer number binary representation? integer number large enough fit in primitive type (java long cannot used). input might represented string format or array of digits. disclaimer, not going solution of production level, don't want use biginteger class. instead, want implement algorithm.
so far ended following approach: input , output values represented strings. if last digit of input even, prepend output "0", otherwise - "1". after that, replace input input divided 2. use method - dividebytwo arithmetical division. process runs in loop until input becomes "0" or "1". finally, prepend input output. here's code:
helper method
/** * @param s input integer value in string representation * @return input divided 2 in string representation **/ static string dividebytwo(string s) { string result = ""; int dividend = 0; int quotent = 0; boolean dividendiszero = false; while (s.length() > 0) { int = 1; dividend = character.getnumericvalue(s.charat(0)); while (dividend < 2 && < s.length()) { if (dividendiszero) {result += "0";} dividend = integer.parseint(s.substring(0, ++i)); } quotent = dividend / 2; dividend -= quotent * 2; dividendiszero = (dividend == 0); result += integer.tostring(quotent); s = s.substring(i); if (!dividendiszero && s.length() != 0) { s = integer.tostring(dividend) + s; } } return result; }
main method
/** * @param s integer in string representation * @return binary integer in string representation **/ static string integertobinary(string s) { if (!s.matches("[0-9]+")) { throw new illegalargumentexception(s + " cannot converted integer"); } string result = ""; while (!s.equals("0") && !s.equals("1")) { int lastdigit = character.getnumericvalue(s.charat(s.length()-1)); result = lastdigit % 2 + result; //if last digit prepend 0, otherwise 1 s = dividebytwo(s); } return (s + result).replaceall("^0*", ""); }
as can see, runtime o(n^2). o(n) integertobinary method , o(n) dividebytwo runs inside loop. there way achieve better runtime? in advance!
try this:
new bigdecimal("12345678901234567890123456789012345678901234567890").tostring(2);
edit:
for making big-number class, may want have @ post week ago. ah, question you, never mind.
the conversion between different number systems in principle repeated "division, remainder, multiply, add" operation. let's @ example:
we want convert 123 decimal base 3 number. do?
take remainder modulo 3 - prepend digit result. divide 3. if number bigger 0, continue number @ step 1
so looks this:
123 % 3 == 0. ==> last digit 0. 123 / 3 == 41. 41 % 3 == 2 ==> second last digit 2. 41 / 3 == 13 13 % 3 == 1 ==> third digit 1. 13 / 3 == 4 4 % 3 == 1 ==> fourth digit 1 again. 4 / 3 == 1 1 % 3 == 1 ==> fifth digit 1.
so, have 11120 result.
the problem need have kind of division 3 in decimal format, not case if don't implement number in decimal-based format (like did in answer last question linked above).
but works converting internal number format external format.
so, let's @ how inverse calculation, 11120 (base 3) decimal equivalent. (base 3 here placeholder arbitrary radix, base 10 placeholder internal radix.) in principle, number can written this:
1 * 3^4 + 1 * 3^3 + 1*3^2 + 2*3^1 + 0*3^0
a better way (faster calculate) this:
((((1 * 3) + 1 )*3 + 1 )*3 + 2)*3 + 0 1 3 4 12 13 39 41 123 123
(this known horner scheme, used calculating values of polynomials.)
you can implement in number scheme implementing, if know how represent input radix (and digits) in target system.
(i added such calculation decimalbigint class, may want calculations directly in internal data structure instead of creating new object (or two) of bignumber class every decimal digit input.)
Comments
Post a Comment