XOR (^) usage in C programs..

Posted on 1999-01-11
Medium Priority
Last Modified: 2010-04-15
  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!!
Question by:Haho
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3

Expert Comment

ID: 1255776
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.
This means set the register AX to zero. This code is some faster as the code AX=0.
LVL 13

Expert Comment

ID: 1255777
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


Author Comment

ID: 1255778
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1255779
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.
LVL 10

Expert Comment

ID: 1255780
If you want more info post more code. With this code Michel's answer is ok.
LVL 84

Expert Comment

ID: 1255781
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

Author Comment

ID: 1255782
Hi guys,

Here is the part from my prog...

        -- code --

      /*************GET THE STRING AND COMPRESS******/

      result = tmpsnd[0] ^ tmpsnd[1];
      a = 2;
      for (;;)
         if (tmpsnd[a] == '#' && tmpsnd[a+1] == '#')
       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

LVL 10

Expert Comment

ID: 1255783
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.

Author Comment

ID: 1255784
' 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

LVL 84

Expert Comment

ID: 1255785
would you understand the code if + had been used in place of ^
LVL 10

Accepted Solution

rbr earned 300 total points
ID: 1255786
Assume the string is "abc##"

result= 'a' XOR 'b' XOR 'c'
this will be the result of the code. As ozo mentioned it's a sum over the string but the operater is not the + it is the ^.

Author Comment

ID: 1255787
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...

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question