• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 532
  • Last Modified:

XOR (^) usage in C programs..

  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!!
  • 4
  • 3
  • 2
  • +3
1 Solution
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.
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

HahoAuthor Commented:
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.
WEBINAR: GDPR Implemented - Tips & Lessons Learned

Join the WatchGuard team on Thursday, March 29th as we recount some valuable lessons learned in weighing the needs of a business against the new regulatory environment, look ahead at the two months left before implementation, and help you understand the steps you can take today!

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.
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
HahoAuthor Commented:
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

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.
HahoAuthor Commented:
' 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 ^
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 ^.
HahoAuthor Commented:
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...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now