Convert from decimal to binary recursion

0

I have to implement a recursive function that, given a decimal number lower than 1024, transforms it into binary, using as an element of storage an array of type char. And it has to show - > Example: 10 --- > 0000001010.

But the recursive function header must be: void conversion (int i, char [] array, int bytes).

Where n is the value to be converted to binary, arrayBin is the array of char initialized to 0 in each position, and nbits the number of bits of our result in binary.

 public void conversion(int i, char [] array, int bytes) {

        final int LIMITE = 1024;

        if(LIMITE == i) {
            throw new IllegalArgumentException("El numero debe ser menor a 1024");
        }

        if(0 == i) return;

        int tmpN = i;
        i /= 2;

        //array[array.length-(++bytes)]= (tmpN % 2 == 0) ? '0' : '1';

        conversion(i, array, bytes);

    }

I found this code but I do not understand much the commented part of code, I do not know if it could be done in another way. For example recursively.

Greetings

    
asked by Patrick 02.04.2018 в 19:28
source

1 answer

2

Greetings, Patrick.

The code that you show us is already a recursive code .

The first thing you need to know about that code are the arguments of the conversion method. As you indicate, the arguments should be n , arrayBin and nbits . In the code, those names are changed by i , array and bytes , but maybe you already knew that.

The second thing is about the simplified version of the if sentence that is used, this form of use is called ternary operator : click here to see an example . In any case, I'll try to explain a bit:

array[index] : This is the common form in which you enter values within an array, using the index (index) of position, you indicate in which position of the array you will enter the assigned value after the operator = .

The position index (index) must always be an integer value . In this case we find this: array.length - (++bytes) . As we can see, this is a mathematical operation, which in the end generates an integer, which will be used as a position index:

++bytes : This is a pre-increment ( this link could help you ) that its function is to increase the value of bytes by 1.

In this way, each execution of the conversion method is entered in each index calculated in the array. But what is stored in those positions?.

Basically, it is what is after the assignment operator = :

(tmpN % 2 == 0) ? '0' : '1'; : This is a operador ternario , really, it fulfills the function of a block if , what is in parentheses is the condition to be checked, the symbol ? indicates the ternary operation, and the two values divided by the symbol : (two points) are the values that will be taken if the condition is true or false , respectively.

Then, the value of tmpN is taken, the module operator (% symbol) is used to calculate the bit (if it is 0 or 1), and if it is equal to 0, it is stored in the index previously calculated the value of 0, otherwise, it will be 1.

Finally, when executing the code, here is the result:

    
answered by 02.04.2018 / 20:04
source