Thread: Error Checking for Recursion Involving Bit Operations

1. Error Checking for Recursion Involving Bit Operations

Hello all-

I'm trying to create a recursive function used for 3D maze traversal. The data for each maze section is stored in a byte. Each bit in the byte, with the exception of the MSB and the 2nd MSB, represents a place where a wall would be. In order to make sure that there are no illegal moves, I need to do error checking using bitwise operations. Let me give an example of what each section of the error checking looks like. After checking, via if and else if respectively, if all three dimension values are 0, or if I've already passed over a space (represented by the MSB), I pull up the else statement:

Code:
```else{
if (0x1 == 3dmaze[i][j][k] & 0x1)
return 0;
else{ if (!function(3dmaze,i-1,j,k)){
//...
} printf("%i %i %i\n", i, j, k); printf("e");maze[i][j][k] = (maze[i][j][k] | 0x80); return 1;
}
}
//The if/else statements continue for each bit going R to L.
//Hex values and i/j/k modifications are also adjusted accordingly.```
This section is specifically where my problem is. The starting coordinate is (1,2,1). For some reason, the program sees this, pulls up the else statement, calculates the new coordinates as (0,2,1), does the process again, and then calculates the new coordinates as (-1,2,1). Naturally this means that the program will crash.

I've tried a debug measure by printing out the hex values in each section, and for some reason they don't make sense.
(ex. the contents of (1,2,1) is 0xffffffdb)
I can see if the values won't compare for that reason, but I know for a fact that the read-in file is not corrupted.

How could I make it so that my program isn't going to keep on making this error?

(I'm working on this issue as I post; if I find a solution before anyone else, I'll be sure to update.)

2. The value 0xffffffdb indicates that the maze array is signed char and you've converted it to int during display - which propagates the MSB as a sign bit and interprets the number as negative. Perhaps you should try defining it as unsigned char.
But beyond that there's not enough code shown to indicate the problem you're having with going beyond the maze borders.

3. Originally Posted by nonoob
The value 0xffffffdb indicates that the maze array is signed char and you've converted it to int during display - which propagates the MSB as a sign bit and interprets the number as negative. Perhaps you should try defining it as unsigned char.
But beyond that there's not enough code shown to indicate the problem you're having with going beyond the maze borders.
When I allocated the space for the maze, I made sure that the array was declared as an unsigned char. There was a time while constructing the recursion statements that I was experimenting with where bitwise operation code went, and I got a warning for these statements provided they weren't the constraints for an if loop. Would I have to do something like this:

Code:
`(0x1 == (unsigned int)maze[i][j][k] & 0x1)`
or maybe something like this:

Code:
`((unsigned char)0x1 == maze[i][j][k] & 0x1)`
I modeled the recursion process based on an in-class example where the array slots were already pre-defined and didn't have to be read in by a file. The recursion process SHOULD work; I'm starting to think that maybe the way the bits are being read is the problem.

4. Maybe you mean
Code:
```if (0x1 == (3dmaze[i][j][k] & 0x1))
...
if (0x2 == (3dmaze[i][j][k] & 0x2))
...
if (0x4 == (3dmaze[i][j][k] & 0x4))```
and so on for bits checking?
```if (3dmaze[i][j][k] & 0x1)