Public Group

#### Archived

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

# xor

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

## 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 on other sites
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 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 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 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 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 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 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 0x04Something.Flags = FLAG_BLAH | FLAG_HM; // It''s BLAH and HMif( Something.Flags & FLAG_BLAH ) cout << "BLAH" << endl; // This WILL be printedif( Something.Flags & FLAG_YAY ) cout << "YAY" << endl; // This will NOT be printedSomething.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 printedif( Something.Flags & FLAG_YAY ) cout << "YAY" << endl; // This WILL be printed

~CGameProgrammer( );

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

##### Share on other sites
The ''X'' in xor stands for ''exclusive'', meaning that only ONE of the bits is on, and not both.

##### Share on other sites
Alright, that answers all of my questions. Thanks everyone

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633338
• Total Posts
3011416
• ### Who's Online (See full list)

There are no registered users currently online

×