• Advertisement

Archived

This topic is now archived and is closed to further replies.

xor

This topic is 5067 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I really don''t understand xor. When would you use it, and what conditions must be satisfied. Strange that I''ve never had to use it, and I''m getting very advanced in programming, but maybe the knowledge would help.... The true general first seeks victory, then seeks battle - Sun Tzu

Share this post


Link to post
Share on other sites
Advertisement
The OR of two boolean values is true if at least one of the values is true. The XOR of two boolean values is true if EXACTLY one of the values is true.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
XOR is really useful.

1 xor 1 = 0
1 xor 0 = 1
0 xor 0 = 0

The cool thing about xor is that if you know one value and the xor''d result, you can get the second value. That is why xor is used in encryption a lot, since you can just do value xor key = encryptedCode, and then to decrypt you just do encryptedCode xor key = value.

Another cool use I''ve seen is to have a doubly-linked list with only one pointer. You take the memory address of the left node and xor it with the memory address of the right node, and store that. Since you always know the previous node in a linked list (since you traverse it linearly), you can just do leftNode xor pointer = rightNode or rightNode xor pointer = leftNode.

Nifty!

Share this post


Link to post
Share on other sites
So xor is when ONLY one value is true, and not both? Thank you, very much It's alot more useful than I thought, considering that you only need one value and the return, to figure out the other. Awsome!



[edited by - Drakkcon on April 11, 2004 6:59:25 PM]

Share this post


Link to post
Share on other sites
You can use XOR to toggle bits in a value, but leave the rest alone. For example to toggle the 3rd bit of an integer A.


A^= 4; // 4= 00000100 in binary

Share this post


Link to post
Share on other sites
Cool, I don''t see how I''ll use that bit of info, but now I know how the logic works

Share this post


Link to post
Share on other sites
A well-know question (but cool if you''ve never seen it before): how do you swap two variables without using a temporary third variable?

a = a XOR b
b = a XOR b
a = a XOR b

Share this post


Link to post
Share on other sites
quote:
Original post by BriTeg
A well-know question (but cool if you''ve never seen it before): how do you swap two variables without using a temporary third variable?

a = a XOR b
b = a XOR b
a = a XOR b

...which is slower than using a third variable, as countless threads on this subject have shown. But it is a neat trick nonetheless.

XOR is often used with bit-flags. For example:

#define FLAG_BLAH 0x01
#define FLAG_YAY 0x02
#define FLAG_HM 0x04

Something.Flags = FLAG_BLAH | FLAG_HM; // It''s BLAH and HM

if( Something.Flags & FLAG_BLAH )
cout << "BLAH" << endl; // This WILL be printed
if( Something.Flags & FLAG_YAY )
cout << "YAY" << endl; // This will NOT be printed

Something.Flags ^= FLAG_BLAH; // Toggle FLAG_BLAH (removes it)
Something.Flags ^= FLAG_YAY; // Toggle FLAG_YAY (adds it)

if( Something.Flags & FLAG_BLAH )
cout << "BLAH" << endl; // This will NOT be printed
if( Something.Flags & FLAG_YAY )
cout << "YAY" << endl; // This WILL be printed


~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..

Share this post


Link to post
Share on other sites

  • Advertisement