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

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!!
0
Haho
Asked:
Haho
  • 4
  • 3
  • 2
  • +3
1 Solution
 
Michel021497Commented:
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.
0
 
MirkwoodCommented:
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

0
 
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hustchCommented:
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.
0
 
rbrCommented:
If you want more info post more code. With this code Michel's answer is ok.
0
 
ozoCommented:
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
0
 
HahoAuthor Commented:
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

0
 
rbrCommented:
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.
0
 
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

0
 
ozoCommented:
would you understand the code if + had been used in place of ^
0
 
rbrCommented:
Assume the string is "abc##"

result= 'a' XOR 'b' XOR 'c'
a=01000001
b=01000010
c=01000011
a^b=00000011
a^b^c=01000000
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 ^.
0
 
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...
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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