使用位掩码提取十六进制的二进制表示会导致错误
Using bitmasks to extract binary representation of hex causes errors
所以我写了这个函数,create_room_connections(struct *, unsigned int)
它将一个指向结构的指针和一个无符号整数作为输入。
现在,那个unsigned int实际上是一个十六进制数,这个函数的目的是用位掩码屏蔽十六进制数,从而推导出它的十进制表示。
这是我的功能:
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);
}
这是结构:
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;
};
我的思路是这样的:
我创建了四个位掩码,
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
我在函数中使用了 4 次以下逻辑(每个位一次):
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.
最后,当我打印出这些值时,
room->econn , room->wconn, room->sconn, room->nconn //4 members of the structure.
我希望它们与十六进制的二进制表示相对应。
例如,如果 hex=13
我希望 (room->econn,room->wconn,room->sconn,room->nconn)=(1101) (binary representation of 13)
但输出完全令人沮丧。
如果我通过 hex=11
,我会得到 1111
。 hex=3
给出 1111
。 hex=10
给出 0000
。 (忽略 if 和 else 块中 printfs 的输出)基本上,在某些情况下,要么所有 if
为真,要么全部为假。
我做错了什么?
编辑:调用代码在一个循环内,就像
create_room_connections(&maze[get_index(i, j, num_cols)], conn);
这里conn
是传递的unsigned int,第一个参数(完全计算后)是指向结构的指针。在主函数中,maze
是一维结构数组,get_index
是将二维数组映射到一维迷宫的函数
如果您查看二元运算符的优先级,如所列:<swansontec.com/sopc.html>;您会注意到“==”运算符的优先级高于“&”运算符,因此首先评估“==”。 – 用户 3629249
所以我写了这个函数,create_room_connections(struct *, unsigned int)
它将一个指向结构的指针和一个无符号整数作为输入。
现在,那个unsigned int实际上是一个十六进制数,这个函数的目的是用位掩码屏蔽十六进制数,从而推导出它的十进制表示。
这是我的功能:
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);
}
这是结构:
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;
};
我的思路是这样的:
我创建了四个位掩码,
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
我在函数中使用了 4 次以下逻辑(每个位一次):
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.
最后,当我打印出这些值时,
room->econn , room->wconn, room->sconn, room->nconn //4 members of the structure.
我希望它们与十六进制的二进制表示相对应。
例如,如果 hex=13
我希望 (room->econn,room->wconn,room->sconn,room->nconn)=(1101) (binary representation of 13)
但输出完全令人沮丧。
如果我通过 hex=11
,我会得到 1111
。 hex=3
给出 1111
。 hex=10
给出 0000
。 (忽略 if 和 else 块中 printfs 的输出)基本上,在某些情况下,要么所有 if
为真,要么全部为假。
我做错了什么?
编辑:调用代码在一个循环内,就像
create_room_connections(&maze[get_index(i, j, num_cols)], conn);
这里conn
是传递的unsigned int,第一个参数(完全计算后)是指向结构的指针。在主函数中,maze
是一维结构数组,get_index
是将二维数组映射到一维迷宫的函数
如果您查看二元运算符的优先级,如所列:<swansontec.com/sopc.html>;您会注意到“==”运算符的优先级高于“&”运算符,因此首先评估“==”。 – 用户 3629249