-1

I need to take an int[][] array and shift bits 0-6 one position to the left, while saving bit 7 and setting bit 0 to the previously saved value of bit 7; for each location [row][col] in the array. I have some code that I feel like is right on the verge of this, but I am missing something and I cannot figure out what:

    int[][] temp = someArray;
    for(int row = 0; row < someArray.length; ++row)
        for(int col = 0; col < someArray [row].length; ++col)
        {
            int num = someArray[row][j];
            int numAtSeven = num & (1 << 7);

            num = num << 1; //shift
            numAtSeven = numAtSeven >> 7; //move to end
            num = num | numAtSeven; //put back in
            System.out.println(row + " = " + col);
            System.out.println(num);
            temp[row][j] = num;
        }
    System.out.println("out");
    someArray = temp;

still ArrayOutOfIndex, produces the following output:

    0 = 0
    279
    0 = 1
    324
    0 = 1
    .
    .
    .
    326 = 1
    359
    326 = 2
    301
    326 = 3
    .
    .
    .

    329
    327 = 499
    357
    out
    java.lang.ArrayIndexOutOfBoundsException: 285

Additionally, I need to take the same array and exchange the bottom 2 bits with the top 2 bits for each [row][col], and I am pretty lost on this portion too. I know that I will need a nested for loop again, and that it will be very similar to, if not a mirror of, the above process but this is really stumping me.

I did search for this first, and found a similar question, but I do not understand that code really either, and it still doesn't answer my first question.

edit: running the code throws a java.lang.ArrayIndexOutOfBoundsException

edit2: updated code to reflect changes

9
  • 1
    You need to state what error you get! Commented Aug 7, 2015 at 7:05
  • how do you calculate height and width? if the second dimension does not have the same size all the time then you might get your IndexOutOfBoundException. Commented Aug 7, 2015 at 7:09
  • oops...I meant to I've added it Commented Aug 7, 2015 at 7:10
  • Maybe this helps: stackoverflow.com/questions/4000169/… Commented Aug 7, 2015 at 7:12
  • @KevinEsche they are defined above in my code, but it is a square array. Each element in the array corresponds to a location [row][col] in a picture, say 256x512 or something. Commented Aug 7, 2015 at 7:15

1 Answer 1

1

Probably because height and width are incorrect and so you get an IndexOutOfBoundsException.

Try:

for(int row = 0; row < someArray.length; ++row)
    for(int col = 0; col < someArray[row].length; ++col)

For a matrix m:

[[0, 0, 0, 0, ... , 0],
 [0, 0, 0, 0, ... , 0],
         ...
 [0, 0, 0, 0, ... , 0]]

m[1][2] would be labeled with M:

m[x][y] --------y----------
        0  1  2  3  ...   m
|  0  [[0, 0, 0, 0, ... , 0],
x  1   [0, 0, M, 0, ... , 0],
|  2           ...
|  n   [0, 0, 0, 0, ... , 0]]

So you might have accidentally used someArray[y][x] instead of someArray[x][y], thus getting the out of bounds exception.

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

3 Comments

that did not work :( it seems to error out here if I step through a break point: someArray[row][col] = entireNum;
@lexIcon still IndexOutOfBounds? Please post the stacktrace.
@lexIcon Also please update your code to reflect any changes you just made.

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.