xor
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
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
"Sneftel is correct, if rather vulgar." --Flarelocke
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!
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!
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]
[edited by - Drakkcon on April 11, 2004 6:59:25 PM]
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
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
a = a XOR b
b = a XOR b
a = a XOR b
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( );
-- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
The ''X'' in xor stands for ''exclusive'', meaning that only ONE of the bits is on, and not both.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement