Solved

XOR (^) usage in C programs..

Posted on 1999-01-11
12
514 Views
Last Modified: 2010-04-15
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
Comment
Question by:Haho
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 1

Expert Comment

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

Expert Comment

by:Mirkwood
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

0
 
LVL 1

Author Comment

by:Haho
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.
0
 
LVL 1

Expert Comment

by:hustch
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.
0
 
LVL 10

Expert Comment

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

Expert Comment

by:ozo
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:Haho
ID: 1255782
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
 
LVL 10

Expert Comment

by:rbr
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.
0
 
LVL 1

Author Comment

by:Haho
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

0
 
LVL 84

Expert Comment

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

Accepted Solution

by:
rbr earned 100 total points
ID: 1255786
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
 
LVL 1

Author Comment

by:Haho
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...
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now