Integer to Binary conversion using the Java programming language.
  1. Home
  2. Posts
  3. Integer to Binary conversion using the Java programming language.
Plano de estudo Publicado Lógico-matemática Inicial

Integer to Binary conversion using the Java programming language.

Plano de estudo voltado a show the solution to a proposed problem using the java programming language.

Write a code fragment that puts the binary representation of a positive integer 'N' into a String 's'.

Juliano Maciel • Lógico-matemática • 11/10/2020
Link permanente
Objetivo educacional

Show the solution to a proposed problem using the Java programming language.

Trilha de estudo

In summary, the approach is:

1 - Decompose the problem and check if all parts were understood (explicit and implicit). 

2 - Define inputs and outputs. 

3 - Determine the procedure (if it's not already in the statement). 

4 - Gather some references if the problem involves related knowledge of other areas.  

5 - Implement the solution in Java with comments to explain each part. 

Decomposing this problem is very easy, it's giving us the input and output:

- Inputs: an integer (int) variable 'N'. 

- Output: a String 's' representing the binary form of the decimal number.

To define a Procedure to solve it's necessary an overview of Positional notation and Numeral Systems.

The definition is taken from a Quora question "What is a positional number system?"

Exemplifying:

In a straightforward manner, it's saying that a decimal number, for example 36, is equal to the sum of two numbers (digits) multiplied by powers of 10.

Reading the number 36, from right to left (units, dozens, etc.), increases the power of the base starting from 10 raised to 0 (i.e, 10^0=1):

36 = (6 x 10^0) + (3 x 10^1)
36 = (6 x 1) + (3 x 10)
36 = 6 + 30
36 = 36

The quantity of numerals (or symbols Indo-Arabic) are the same as the base.

For example, base 10 has { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } digits.

The rule for the symbols is: start from 0 until base - 1.

So the base two, starting from 0 until (2 - 1) has { 0, 1 }.

From right to left, the representation of the binary number 10102 in base ten is:

10102 = (0 x 2^0) + (1 x 2^1) + (0 x 2^2) + (1 x 2^3)
10102 = (0 x 1) + (1 x 2) + (0 x 4) + (1 x 8)
10102 = 0 + 2 + 0 + 8
10102 = 10

With all said, the problem is asking us to implement an algorithm to make the inverse of the example above, i.e, given a number in base decimal convert it to binary:

10 = 10102.

Now, it's necessary to understand very little about Number theory basics: Euclid's Division Algorithm.

In summary, It's about multiples and divisors of integer numbers and states the follow:

The above explanation is the follow theorem:

For any natural numbers n and m, with m != 0 (not null), there exists only one pair of numbers q and r such that n = ( m * q ) + r.

Let's use numbers to see that:

These concepts shows that any number n (n > 1) in a base b, with m being the numerals {0, 1, 2, ..., (b - 1)}, can be represented uniquely as:

n = m0 + m1 * b^1 + m2 * b^2 + ... + mi * b^i ( i >= 0 and mi != 0)

or

n = ( b * q ) + m0

representing ( b * q ) as:

b * q ) = m1 * b^1 + m2 * b^2 + ... + mi * b^i

using the distributive property:

m1 * b^1 + m2 * b^2 + ... + mi * b^i = b * (m1 + m2 * b^1 + m3 * b^2 + ... + mi * b^i - 1)

we reach the form:

n = b * (m1 + m2 * b^1 + m3 * b^2 + ... + mi * b^i - 1) + m0

with all mi's = {0, 1,..., (b - 1)}.

Plugging the example numbers:

36 = 10 * (3) + 6 {m0=6 and m1=3}

365 = 10 * (6 + 3 * 101) + 5 {m0=5, m1=6 and m2=3}

10102 = 2 * (1 + 0 * 2^1 + 1 * 2^2) + 0 {m0=0, m1=1, m2=0 and m3=1}

So, to convert any number n (written in base ten) to binary, it's necessary that its representation be of the form:

n = 2 * (m1 + m2 * b^1 + m3 * b^2 + ... + mi * b^i - 1) + m0

with all mi's = {0, 1}.

Doing that is just a matter of using successive divisions by the base we wish to convert keeping each remainder and the last quotient until it's {0, 1}:

36 = ????2

Start dividing by the base:

36 / 2 = 18 (remainder=0, that's the m0 digit)

The quotient is 18 not {0, 1}, keep dividing by the base:

18 / 2 = 9 (remainder=0, that's the m1 digit)

The quotient is 9 not {0, 1}, keep dividing by the base:

9 / 2 = 4 (remainder=1, that's the m2 digit)

The quotient is 4 not {0, 1}, keep dividing by the base:

4 / 2 = 2 (remainder=0, that's the m3 digit)

The quotient is 2 not {0, 1}, keep dividing by the base:

2 / 2 = 1 (remainder=0, that's the m4 digit)

Finally, the quotient is 1 (the m5 digit).

All the mi's are m0=0, m1=0, m2=1, m3=0, m4=0, m5=1.

36 = 2 * (m1 + m2 * 2^1 + m3 * 2^2 + m4 * 2^3 + m5 * 2^4) + m0
36 = 2 * (0 + 1 * 2^ 1 + 0 * 2^2 + 0 * 2^3 + 1 * 2^4) + 0
36 = 2 * (0 + 2 + 0 + 0 + 16) + 0
36 = 2 * (18) + 0
36 = 36

Representing as {0, 1} it's just a matter of concatenating all mi's (from right to left):

36 = m5m4m3m2m1m0 = 1001002

Implementing this procedure as a static method in Java:


Obs.: The problem statement does not specify, but this solution can be implemented with recursion and a Stack, LIFO (last-in-first-out). 

The test case is very simple:

Materiais e recursos
Referências
Continuidade dialética
Como este conhecimento evolui:
Tese Antítese Síntese

Este post é a tese. Leituras críticas e sínteses derivadas podem ampliar sua maturidade.


Antíteses desta tese

Carregando...

Sínteses geradas

Carregando...
Metadados do recurso
Tipo de documento: Anotação
Formato: Text.
Requisitos técnicos:
Basic Mathematics, Programming Logic, Java Programming Language.
Status: Publicado
Score: 80
Maturidade: Inicial
Apoio de IA: Não