Thread: Error Checking for Recursion Involving Bit Operations

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    32

    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. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    Quote Originally Posted by nonoob View Post
    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.
    Last edited by William Putnam; 12-19-2012 at 01:44 PM. Reason: fixed formatting tags

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    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?
    Read about operator precedence.
    And just write
    Code:
    if (3dmaze[i][j][k] & 0x1)
    ...
    if (3dmaze[i][j][k] & 0x2)
    ...
    if (3dmaze[i][j][k] & 0x4)
    and so on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error involving char type
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 05-18-2007, 05:54 PM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  3. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  4. Strange error involving rand()
    By Samus Man in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 08:40 PM
  5. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 05:20 PM