C:对char进行I和^操作的结果是什么?

agn*_*zka 0 c bitwise-operators

有一段代码:

int p(char *a, char*b)
{
  while (*a | *b) 
  {
   if (*a ^ *b)
    //...
  }
}
Run Code Online (Sandbox Code Playgroud)

我真的不知道它在做什么.

编辑:我理解|^操作员做什么,我只是不知道他们将如何处理char价值观.

Sim*_*rim 8

虽然字符串a或字符串b没有用完字符,但请检查它们是否不同.

int p(char *a, char*b)
{
     // While both string a and string b have characters left
     // note this assumes they are both zero terminated
     // and if not the same length they have trail zeros
     while (*a|*b)  
     {
         // check to see if the character is different
         // this is done via the xor
         if (*a^*b)
              //(...)
         }

         // should increment pointers or will never exit the loop
         // a++;
         // b++;
      }
Run Code Online (Sandbox Code Playgroud)


小智 6

它将它们视为小整数.| 然后,运算符执行OR,并且^运算符对构成整数的各个位执行异或(异或).对于大多数基于字符的应用程序,这两种操作都不是特别有用,但可以使用它们(例如)在comms编程中为char添加奇偶校验位.