0

So I wrote this function, create_room_connections(struct *, unsigned int) and it takes as input a pointer to a structure, and an unsigned int.

Now, that unsigned int is actually a hexadecimal number, and the purpose of this function is to mask the hexadecimal number with a bitmask, and thereby deduce its decimal representation.

This is my function:

void create_room_connections(struct maze_room *room, unsigned int hex) {
  unsigned int emask=8;
  unsigned int wmask=4;
  unsigned int smask=2;
  unsigned int nmask=1;
  room->visited=0;                                          // set visted to false
// printf("HEXXXXX=%d\n",hex);
// printf("hex&emask=%d\n",hex&emask);
// printf("hex&wmask=%d\n",hex&wmask);
// printf("hex&smask=%d\n",hex&smask);
// printf("hex&nmask=%d\n",hex&nmask);
// printf("emask=%d\n",emask);
// printf("wmask=%d\n",wmask);
// printf("smask=%d\n",smask);
// printf("nmask=%d\n",nmask);


  if(hex&emask == emask)    {                       //set econn to 1
     room->econn=1;
     printf("emask true!,so econn=%d\n",room->econn); 
 }
  else {
     room->econn=0;
     printf("emask false!,so econn=%d\n",room->econn);
 }

  if(hex&wmask == wmask) {                          //set wconn to 1
     room->wconn=1;
     printf("wmask true!, so wconn=%d\n", room->wconn);
 }
  else {
     room->wconn=0;
     printf("wmask false!,so wconn=%d\n",room->wconn);
 }

  if(hex&smask == smask)    {                       //set sconn to 1
     room->sconn=1;
     printf("smask true!,so sconn=%d\n",room->sconn);
 }
  else {
     room->sconn=0;
     printf("smask false!,so sconn=%d\n",room->sconn);
 }

  if(hex&nmask == nmask)    {                       //set nconn to 1
     room->nconn=1;
     printf("nmask true!,so nconn=%d\n",room->nconn);
 }
  else {
    room->nconn=0;
    printf("nmask false!,so nconn=%d\n",room->nconn);
}

// printf("econn, wconn, sconn, nconn=%d %d %d %d\n",room->econn, room->wconn, room->sconn, room->nconn);
}

This is the structure:

struct maze_room {
  int row;
  int col;
  int visited;          //0 for false, 1 for true, 
  int econn;              // 1 for wall and 0 for opening
  int wconn;
  int nconn;
  int sconn;
};

My line of thinking is as follows:

I have created four bitmasks,

unsigned int emask=8;  //to find out the first bit (most significant) corresponds to econn
unsigned int wmask=4;  //to find out the second bit,corresponds to wconn
unsigned int smask=2;  //to find out the third bit, corresponds to sconn
unsigned int nmask=1;  //to find out the last bit (least significant) corresponds to nconn

I use the following logic 4 times in the function (once for each bit):

if(hex&emask == emask)      // hex is the unsigned int passsed as input. if it has a 1 as its first bit, the if would evaluate to true, and then I set the econn to 1
   room->econn=1;           // econn is an int member of the structure.

In the end, when I print out the values,

room->econn , room->wconn, room->sconn, room->nconn   //4 members of the structure.

I expect them to correspond with the binary representation of hex.

For example, if hex=13 I expect (room->econn,room->wconn,room->sconn,room->nconn)=(1101) (binary representation of 13)

But the output is completely frustrating.

If I pass hex=11, I get 1111. hex=3 gives 1111. hex=10 gives 0000. (disregarding the output of the printfs inside the if and else blocks) Basically, in some cases either all the if's are true or all are false.

What am I doing wrong?

EDIT: The calling code is inside a loop, and it goes like

    create_room_connections(&maze[get_index(i, j, num_cols)], conn);

Here conn is the unsigned int that is passed, and the first param (after completely evaluated) is the pointer to the struct. In the main function, maze is 1d array of structs, and get_index is a function that maps a 2d array to the 1d maze

tofu
  • 125
  • 1
  • 3
  • 11
  • 1
    Show also the exact calling code that exhibits this issue – Sami Kuhmonen Sep 19 '15 at 16:37
  • @SamiKuhmonen added calling code – tofu Sep 19 '15 at 16:51
  • Also the exact value to `conn`. Is the `hex=10` actually decimal 10 or hexadecimal 10? The latter would naturally cause the four lower bits to be all zero, but wouldn't explain the others. – Sami Kuhmonen Sep 19 '15 at 16:54
  • declaration for conn is `unsigned int conn=0` and I read it like so: `fscanf(mazefile, "%1x", &conn);` where mazefile is the file from where I read it. All numbers in mazefile are hex – tofu Sep 19 '15 at 16:57
  • @SamiKuhmonen In this case wouldn't the `"%1x"` format specifier convert a hex into a decima? – tofu Sep 19 '15 at 17:01
  • Why don't you check the `hex` value received in the function on every call, vs. the corresponding value in the file? Is this a text file? – Leeor Sep 19 '15 at 17:04
  • @Leeor I actually did that. If you see my function, I have commented out the line `printf("HEXXXXX=%d\n",hex);` . Both the values match . Yes it is a text file, and consists of a 2d array of hexadecimal numbers – tofu Sep 19 '15 at 17:05
  • 1
    1) Insure proper header fiels are used 2) insure all compiler warnings are enable and taken care of. 3) Use `"%x"` with `unsigned`. 4) `"%1x"` format specifier convert a hex into a decimal?" No. On printing, it converts an `unsigned` to hexadecimal characters - at least 1. On scanning, it converts 1 hexadecimal character to an `unsigned` value. – chux - Reinstate Monica Sep 19 '15 at 17:12
  • 1
    @chux. You were right. I enabled the flag -Wall, to get all warnings and gcc suggested I should parenthesize the `&` operation. So I changed `if(hex&wmask == wmask)` to `if((hex&wmask) == wmask)`. And it works like a charm! But I have no idea why. @Leeor, @ SamiKuhmonen, any ideas? – tofu Sep 19 '15 at 17:30
  • @ryyker, though I know that there are far simpler approaches to accomplish this simple feat, I am constrained to use bitmasking – tofu Sep 19 '15 at 17:32
  • 2
    if you look at the Precedence of the binary operators, as listed at: you will notice that the '==' operator has a higher Precedence than the '&' operator, so the '==' is evaluated first. – user3629249 Sep 19 '15 at 17:45
  • got it guys. the `&` has a lower priority than the `==` – tofu Sep 19 '15 at 17:45
  • @user3629249 I was just about to answer my own question, but you beat me to it. Thanks anyway. You can go ahead and answer it. That was the gotcha. It works correctly now – tofu Sep 19 '15 at 17:45

1 Answers1

1

if you look at the Precedence of the binary operators, as listed at: <swansontec.com/sopc.html>; you will notice that the '==' operator has a higher Precedence than the '&' operator, so the '==' is evaluated first. – user3629249

Armali
  • 18,255
  • 14
  • 57
  • 171