views:

199

answers:

8

There is a piece of code:

int p(char *a, char*b)
{
  while (*a | *b) 
  {
   if (*a ^ *b)
    //...
  }
}

and I don't really know what it's doing.

Edit: I understand what the | and ^ operators do, I just don't know what they'll do with char values.

+8  A: 

While the string a or string b have not run out of characters, check to see if they are different.

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++;
      }
Simeon Pilgrim
ok but what is the exact result of *a|*b? does just do a bitwise operation on two binary numbers that represent the chars?
agnieszka
Hoping, of course, that there are some ++'s missing in the copied code.
Michael Brewer-Davis
@angieszka: Yes, indeed.
Mehrdad Afshari
@Michael - good point, otherwise it never returns
Simeon Pilgrim
AlbertEin
wow just noticed I missed the questions point in title vs context of body
Simeon Pilgrim
*a | *b doesn't assume that both of the strings have characters left. It assumes that at least one of them (appear to) have characters left.Using *a ^ *b for "are they different" obfuscates and is a poor choice of operator when != would work just as well and make intent clear.
dash-tom-bang
A: 

That's the XOR operator, which means "exclusive OR". It operates bitwise.

for each bit in your item:

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

dash-tom-bang
+4  A: 

| is the bitwise OR operator. ^ is the bitwise XOR operator:

    10101010      11010101  
  | 01010100    ^ 11111110
  ==========    ==========
    11111110      00101011

While char can be used to represent characters, it's inherently an integer data type. It stores a binary number (just like everything else in a binary digital computer).

Mehrdad Afshari
+2  A: 

In this case, char is just interpreted as a number and | and ^ are the bitwise operators OR and XOR, respectively.

Naaff
+6  A: 

It treats them as small integers. The | operator then does an OR and the ^ operator does an XOR (exclusive or), on the individual bits making up the integers. Neither operation is particularly useful for most character-based applications, but they can be used (for example) to add a parity bit to a char in comms programming.

anon
You can call me Chuck
They don't work. TO_LOWER for example turns the NUL character into a space.
anon
A: 

| = Bitwise OR (inclusie OR)

^ = Bitwise XOR (exclusive OR)

Sev
A: 

They will be treated as the binary representation. This typically means unsigned 1 byte. Not true for all architectures!

(*a|*b) means either *a or *b or both contain anything else than '\0'

(*a^*b) means that the two characters are not identical.

dwc
(*a|*b) means that BOTH of them don't equal 0, not either or.
kitchen
dwc
+2  A: 

The code just means:

while (*a != '\0' && *b != '\0')
   if (*a != *b)

The developer wanted to be clever using bitwise operators with the chars

AlbertEin
a and b should be *a and *b, and the if is just *a != *b, not including the nul comparisons.
ysth
You're rigth, i was confused, thanks
AlbertEin