Reputation: 125
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
Upvotes: 0
Views: 117
Reputation: 19395
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
Upvotes: 1