C语言中的异或运算
我在C中的XOR操作已经面临了几个小时的问题 我试图对两个字符*的内容进行异或运算C语言中的异或运算,c,char,xor,C,Char,Xor,我在C中的XOR操作已经面临了几个小时的问题 我试图对两个字符*的内容进行异或运算 char* toto = malloc(sizeof(char)*5); char* bob = malloc(sizeof(char)*5); 它们都只包含0和1,每个都有5个插槽 toto的printf返回00011,bob返回11111 这是我最后尝试对两个值进行异或运算的结果 首先,我按步骤进行: 1-将它们转换为一个int值 正如您所看到的,bob中的0已消失,没有问题 2-在各自相同的位之间使用异或
char* toto = malloc(sizeof(char)*5);
char* bob = malloc(sizeof(char)*5);
它们都只包含0和1,每个都有5个插槽
toto的printf返回00011,bob返回11111
这是我最后尝试对两个值进行异或运算的结果
首先,我按步骤进行:
1-将它们转换为一个int值
正如您所看到的,bob中的0已消失,没有问题
2-在各自相同的位之间使用异或运算符^
int result = val_toto ^ val_bob;
问题来了:
printf("%d", result) => 11116
异或运算的结果应该是
11111
^ 00011 // the 0 are taken care of even if not showed in printf
-------
11100 // but I get 1116 ???
真正的问题是,只有当我对托托和鲍勃使用这种组合时,问题才会出现。XOR操作适用于所有这些操作,例如:
toto => 00101 // as char*
bob => 000001
//then I use atoi() with each of them
toto => 101 // as int
bob => 1
因此,我得到了预期的结果:
101 XOR 1 => 100 //as int
你知道为什么它在上面的组合中工作得很好,但在我有11111的时候却从来没有
谢谢atoi和printf的%d说明符都假定您处理的是十进制格式的数字,而不是二进制。进行异或计算时,假设11111和00011是二进制的。atoi和printf的%d说明符都假设您处理的是十进制格式的数字,而不是二进制的。进行异或计算时,假设11111和00011是二进制的。执行int result=val_toto^val_bob;,您使用的是具有两个十进制数的xor运算符,而不是二进制数。00011在十进制中不是3而是11。要更正此错误,请尝试我的代码:
char *str="1100"; // in binary now convert it to decimal so it became 12
char *endptr=NULL; // to detect any mistake
int num = strtol(str, &endptr, 2); // strtol meams str to long 2 (binary base )
if (endptr!=NULL) { printf("error\n"); exit(1); }
printf("%d\n", num); // will print 12. now you can use it with xor
执行int result=val\u toto^val\u bob;,您使用的是具有两个十进制数的xor运算符,而不是二进制数。00011在十进制中不是3而是11。要更正此错误,请尝试我的代码:
char *str="1100"; // in binary now convert it to decimal so it became 12
char *endptr=NULL; // to detect any mistake
int num = strtol(str, &endptr, 2); // strtol meams str to long 2 (binary base )
if (endptr!=NULL) { printf("error\n"); exit(1); }
printf("%d\n", num); // will print 12. now you can use it with xor
该问题已在评论和报告中指出 对于解决方案,除非您完全需要,否则根本不需要转换为int,您可以在每个字符中执行XOR,并将结果保存在新的字符数组中,如下所示:
char toto[] = "11111";
char bobo[] = "00011";
size_t len = strlen(toto); // use strlen only if toto is null terminated
// otherwise use the size of the array
char result[len];
for(size_t i = 0; i < len; i++) // same here
{
result[i] = (toto[i] ^ bobo[i]) + '0';
}
printf("%s", result); // again if the arrays are null terminated,
// otherwise print it in a for cycle
// using the array size
如果需要,您始终可以使用strtol安全地转换最终值:
输出:
11100
28
end和errno上挂起的错误检查
该问题已在评论和报告中指出 对于解决方案,除非您完全需要,否则根本不需要转换为int,您可以在每个字符中执行XOR,并将结果保存在新的字符数组中,如下所示:
char toto[] = "11111";
char bobo[] = "00011";
size_t len = strlen(toto); // use strlen only if toto is null terminated
// otherwise use the size of the array
char result[len];
for(size_t i = 0; i < len; i++) // same here
{
result[i] = (toto[i] ^ bobo[i]) + '0';
}
printf("%s", result); // again if the arrays are null terminated,
// otherwise print it in a for cycle
// using the array size
如果需要,您始终可以使用strtol安全地转换最终值:
输出:
11100
28
end和errno上挂起的错误检查
atoi转换十进制而非二进制的整数。您使用的是11111 base 10,而不是11111 base 2。第一个是一万一千零零元;第二个是31。你在对十进制数进行运算,就好像它们是二进制数一样,所以这不会很好地工作……char*toto=mallocsizeofchar*5;->char*toto=mallocsizeofchar*6;谢谢你的回答,我明白我做错了什么。我的代码中还有大小为5+1的malloc,只是忘了在这里写它,谢谢提醒:atoi转换以十进制而不是二进制编写的整数。您使用的是11111 base 10,而不是11111 base 2。第一个是一万一千零零元;第二个是31。你在对十进制数进行运算,就好像它们是二进制数一样,所以这不会很好地工作……char*toto=mallocsizeofchar*5;->char*toto=mallocsizeofchar*6;谢谢你的回答,我明白我做错了什么。我的代码中还有大小为5+1的malloc,只是忘了在这里写,谢谢提醒:谢谢你的示例,比我干净多了did@Wapitix,很高兴帮忙。谢谢你的样品,比我干净多了did@Wapitix,很高兴提供帮助。谢谢你的回答:谢谢你的回答: