0

I need to get the binary representation for a range of numbers inside a matrix in order to perform some operations with another vector.

So let's say I will get the binary representation for 2^4 numbers, that's it from 0 to 15. I know I need a 16x4 matrix.

I've got this code:

int [][] a = new int[15][4];

for (int i = 0; i < a.length; i++) {
    a[i] = String.format("%5s", Integer.toBinaryString(i)).replace(' ', '0').toCharArray();
}

So, being the array representation of the binary formatted number a char[], I can't just asign it to a[i].

If there any way to perform a cast without looping through the char array?

1
  • 2
    You mean a 16 x 4 ? =) why 5 ? Commented Nov 14, 2013 at 19:51

3 Answers 3

2

Not that I am aware of. There are some different ways you can do it, either looping through the integer representation of the binary string, and the taking num%10 and num/10 for every step, if you absolutely don't want a loop through the char array. However in this case it seems pretty straight forward to just loop through the char array. Anyways here is the solution, in the way you didn't want it I guess...

int [][] a = new int[16][4];

for (int i = 0; i < a.length; i++) {
    char[] cArr = String.format("%4s", Integer.toBinaryString(i)).replace(' ', '0').toCharArray();
    for(int j = 0; j < a[0].length; j++)
        a[i][j] = Integer.parseInt(cArr[j]+"");

}
Sign up to request clarification or add additional context in comments.

Comments

1

This is a simpler solution to what you are trying to accomplish...

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[0].length; j++) {
            a[i][a[0].length - 1 - j] = (i & (1 << j)) != 0 ? 1 : 0;
        }
    }

Instead of converting an integer i to String and then replacing white spaces with zeros and then converting it to array, you:

  1. Take i.

  2. Take a binary number A with the only 1 at j-th position (other being zeros): A = (1 << j)

  3. Perform conjunction (binary bit-wise multiplication) of your number and the number A. This is accomplished by: (i & A)

  4. If there was non-zero bit at that position, after conjunction you will get A. If there was a zero bit, you will get 0.

  5. If the result is not zero, i has non-zero bit in j-th position. Otherwise it has zero there.

The solution using bit-wise operations will be faster too.

1 Comment

Works great! I only wonder, is there any chance I can get the numbers in the right order? Being a.length 16 I get the following ouput when printing the matrix: 0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15
0

I believe that one outer loop will still be required to iterate through char[][] rows.

int[] charArray2intArray(char[][] binary) {

    int[] numbers = new int[binary.length];
    int row = 0;
    for (char[] number: binary) {

        String bin = new String(number);
        numbers[row++] = Integer.parseInt(bin, 2);
    }

    return numbers;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.