Challenge: Identify ways to use XOR logical operator - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html) +--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html) +--- Thread: Challenge: Identify ways to use XOR logical operator (/thread-1277.html) |
Challenge: Identify ways to use XOR logical operator - Moneo - 06-21-2003 Many people never use exclusive-or (XOR). A professor at NYU, who had a PHD in Computer Science, while giving a class on "C" programming in 1985, described how it worked and then said "But who knows why you would ever use it." I almost had a fit. Anyway, I sure you guru's can come up with many uses. But, let's keep them practical, i.e., give examples that make sense and are really useful. Post a description of how you're using the XOR, and then give a coding example, which will usually be one line of code. Try not to be too critical of post submitted by others, and let's keep the BS down. Thanks, Moneo ***** Challenge: Identify ways to use XOR logical operator - seph - 06-21-2003 I'm sure you can use them somehow to make a "negative image" on the screen... Like turn all colours the opposite of what they are (really just 255-colour%). Then again, I proved myself wrong. To Seph: - Moneo - 06-21-2003 You mean like flip the bits in color%, by doing: Code: color% = color% xor 255 If that's what you want, this will do it. ***** Challenge: Identify ways to use XOR logical operator - seph - 06-21-2003 Sure that works too. I win! Challenge: Identify ways to use XOR logical operator - LooseCaboose - 06-21-2003 Circuit construction, adder circuits use a XOR gate for the sum value. ie. Code: A B | S C Bitflipping, as mentioned by Seph. Taking a n-bit number and XORing it against a value where all the bits are set will flip all the bits in the number. Anytime you need to determine if one or the other, but not both of two values is set. Although many programmers will simply write this long hand: Code: if A then Challenge: Identify ways to use XOR logical operator - DrV - 06-21-2003 Swapping integers... Code: a% = 3 To LooseCaboose: - Moneo - 06-21-2003 Good example re circuit construction. Your second example about doing things the long way reminds me of my favorite example which is for using a flip/flop switch. If the switch is on (a one), you want to turn it off. If the switch is off (zero), you want to turn it on. Code: flipflop = flipflop xor 1 ***** To drV: - Moneo - 06-21-2003 Excellent. Your example is a classic. You must be an assembly language programmer. In assembler you find your self doing this method to swap the contents of two registers without having to use another intermediate register or time-consuming stores to memory. In a sample assembly language, to swap registers a and b, it would look like this: Code: a xor b ***** Challenge: Identify ways to use XOR logical operator - oracle - 06-21-2003 Edit Button! *dissolves into tears* Challenge: Identify ways to use XOR logical operator - toonski84 - 06-21-2003 actually, moneo's example, by test, is an eensy weensy pee widdle bit slower than n% = 1 - n% for 100000000 operations: 1-n: 11.19995 seconds xor: 11.26001 seconds |