【问题标题】:XOR operation in CC中的异或运算
【发布时间】:2021-07-19 23:52:56
【问题描述】:

我在 C 语言中进行 XOR 运算时遇到了几个小时的问题。

我正在尝试对两个char * 的内容进行异或运算。

char* toto = malloc(sizeof(char)*5);
char* bob = malloc(sizeof(char)*5);

它们都只包含 01,每个都有 5 个插槽。

totoprintf 返回 00011bob 返回 11111

printf("%s", toto) => 11111
printf("%s", bob)  => 00011

这是我最终尝试异或的两个值。

首先我按步骤进行:

1- 将它们中的每一个翻译成int

int val_toto = atoi(toto); 
int val_bob = atoi(bob);

// printf("%d", val_toto) => 11111
// printf("%d", val_bob)  => 11

你可以看到 bob 中的0 消失了,没问题。

2- 在它们各自相同的位之间使用 XOR 运算符 ^

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 ???

真正的问题是,只有当我将该组合用于totobob 时,才会出现问题。 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 转换以十进制而不是二进制写入的整数。你有 11111 base 10 而不是 11111 base 2。第一个是一万一千和变化;第二个是 31。
  • 你在对十进制数进行操作,就好像它们是二进制数一样,所以这不会很好......
  • char* toto = malloc(sizeof(char)*5); -> char* toto = malloc(sizeof(char)*6);
  • 感谢您的回答我明白我做错了什么。我的代码中也有大小为 5+1 的 malloc,只是忘记在这里写了,谢谢提醒 :)

标签: c char xor


【解决方案1】:

atoi 和 printf 的 %d 说明符都假定您正在处理十进制格式的数字,而不是二进制。当您进行 XOR 计算时,您假设 11111 和 00011 是二进制的。

【讨论】:

    【解决方案2】:

    当您执行int result = val_toto ^ val_bob; 时,您使用的是带有两个十进制数的异或运算符,而不是二进制数。 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 
    

    【讨论】:

    • 感谢您的回答:)
    【解决方案3】:

    问题已经在 cmets 和 David Grayson answer 中指出。

    对于解决方案,除非您绝对需要,否则您根本不需要转换为 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
    

    输出:

    11100
    

    如果需要,您始终可以使用 strtol 安全地转换最终值:

    char *end;
    printf("%ld", strtol(result, &end, 2));
    

    输出:

    28
    

    正在等待对 enderrno 的错误检查。

    Live sample

    【讨论】:

    • 谢谢你的样品,比我做的干净多了
    • @Wapitix,很高兴为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    相关资源
    最近更新 更多