Link to home
Start Free TrialLog in
Avatar of Haho
Haho

asked on

XOR (^) usage in C programs..

hi,
  I hv a sample line:
   result = result ^ var2[I];

The above is just a sample.. While I know what XOR means,
I do NOT understand its usage and its application..
my questions are:
1. wat does the above line is used for?
2. wat should we use ^ for??? when??

Code examples would be great in illustrating its uses and functions... thanks!!
Avatar of Michel021497
Michel021497

1) If you have 2 boolean variables and a piece of code, you for example want the code to run only if one of the 2 Booleans is true.

2) It could also be used for bit testing.
If ((var1 XOR var2) == 0001)
{
  run code;
}

the code only runs if the most right bit is not equal and other bits are equal.

3) for assembly programming.
XOR AX, AX
This means set the register AX to zero. This code is some faster as the code AX=0.
XOR is used for bitwise operations.
0 becomes 1 and 1 becomes 0
Usually bitwise operations are used for encryption, compression and graphical purpose.
In drawing for example, it is useful to use xor, since xor twice will give you the original value again.
(a ^ b) ^ b = a

Avatar of Haho

ASKER

thanks Michel but your answer is too general... perhaps my question is vague..but I would only like to focus on C programs...

A more detailed answer and code examples would be great... as mentioned, perhaps a example taken from compression (!!!- because my prog is a compression program I think) ... thanks again ...I am increasing the points cause I would like to depen my understanding of '^' in C.
XOR has many uses, here are a few examples :
1) To change one or more bits. Often, when you have some constants, that can be or'd together to give the desired options for a function, there also is one for them all. A easy way to get all except one is UseOptions = AllOptions xor SpecificOption
2) If you XOR with the same value twice, the result is equal to the original value. You can use this to invert something on an image, and then change it back by doing a 2nd xor.
3) As a curiosity, you can use it to xchange values in two variables. The following are equivalent :
      x = a; a = b; b = x;
      a = a xor b; b = b xor a; a = a xor b;
   You can use the last one, if you have nowhere to store x.
If you want more info post more code. With this code Michel's answer is ok.
Avatar of ozo
if I saw the above sample line in a compression program, my first guess would be that it was part of a CRC computation.
Which is sort of like a checksum based on arithmetic modulo 2, and can be used to detect or correct random bit errors during transmission
Avatar of Haho

ASKER

Hi guys,

Here is the part from my prog...

        -- code --

      /*************GET THE STRING AND COMPRESS******/
      ldchar(tmp,519,tmpsnd);

      result = tmpsnd[0] ^ tmpsnd[1];
      a = 2;
      for (;;)
      {
         if (tmpsnd[a] == '#' && tmpsnd[a+1] == '#')
       break;
       result =  result ^ tmpsnd[a];
       a= a+1;
      }

      sprintf (snd,"%s%c",tmpsnd,result);

        -- rest of code --

Frankly speaking, I hv no idea what the heck it is doing! :)
I know C quite well but I am no expert..

thanks and keep those comments/answers coming

tmpsnd is a string to be send over the stream snd. And to check if an error had occured during the sending a control info is send with the string and is build by XORing all bytes of the string. The last 2 characters will not be used for the checksum and the last must be a # otherwise the loop will never end.
Avatar of Haho

ASKER

' and is build by XORing all bytes of the string.'
how specifically does it work??? can anyone run me through on the code...
perhaps using a simple string example...? Sorry for being dense..
I am increasing the points again, rbr, u r welcome to elaborate on your answer

would you understand the code if + had been used in place of ^
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Haho

ASKER

so basically it is to provide a checking sum (checksum?) before sending the data over a network, (say), and then rechecking it at the receiving side...
thanks