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 - whitetiger0990 - 06-22-2003 i think this Code: flop = -flop + 1 Code: flipflop = flipflop XOR 1 How to do XOR when your language doesn't have this operator? - Moneo - 06-22-2003 For those of us that prefer using the XOR operator, it comes as a shock when programming in another language that does not support this operator. Assuming that this language does have AND/OR/NOT operators, we can emulate an XOR as follows: Code: ´The following performs the same as: result = a XOR b Challenge: Identify ways to use XOR logical operator - DrV - 06-23-2003 Quote:XOR is good, but it's a crappy name. ummm... eXclusive OR? one or the other but not both? Sample reason for using XOR. - Moneo - 06-23-2003 We have posted several examples of XOR for flipping the bits in a byte. Here's a reason I had to do that. I was writing a report program whose input file was sorted on 3 keys of variable ASCII bytes. Regardless of where a sorted file comes from, I ALWAYS sequence check it while I'm reading it, otherwise the output becomes garbage and my program looks like an idiot. The problem was that the 2nd key was sorted descending and the 1st and 3rd keys were ascending. How do you do a sequence check in this case? Normally, you concatenate the 3 keys into a combined key and just compare each combined key to the preceding for sequence, but the descending key in the middle would not work. After thinking about it for a long while, I came up with the idea of doing an XOR with 255 (hex FF) on every byte of the descending second key before placing it into the combined key. Eureka, it worked! ***** My opinion on the scoring for this chalenge: - Moneo - 06-23-2003 I initiated this post so I guess I should issue some final scores. There were 11 people posting (including myself), but only 6 of us actually contibuted something, in my opinion. I have assigned 1 point for concept and 1 point for a coding example. Here are the scores in posting order for the contributors: SEPH......................... 1 point LOOOSECABOOSE..... 2 points DRV........................... 2 points TOONSKI................... 1 point ANTONI..................... 4 points (3rd example not understood) MONEO..................... 1 point ***** Update: - Moneo - 07-03-2003 In the Challenge topic re Recursion, we also saw the following use of XOR. Code: REM Generate Gray Code bytes; i.e. values in range from 0 to 255. |