Moneo
|
 |
« on: June 20, 2003, 11:12:02 PM » |
|
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 *****
|
|
|
Logged
|
|
|
|
seph
|
 |
« Reply #1 on: June 20, 2003, 11:20:33 PM » |
|
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.
|
|
|
Logged
|
earn.
|
|
|
Moneo
|
 |
« Reply #2 on: June 20, 2003, 11:41:46 PM » |
|
You mean like flip the bits in color%, by doing: color% = color% xor 255
If that's what you want, this will do it. *****
|
|
|
Logged
|
|
|
|
seph
|
 |
« Reply #3 on: June 20, 2003, 11:43:56 PM » |
|
Sure that works too. I win!
|
|
|
Logged
|
earn.
|
|
|
LooseCaboose
|
 |
« Reply #4 on: June 21, 2003, 12:23:27 AM » |
|
Circuit construction, adder circuits use a XOR gate for the sum value. ie. A B | S C ----+---- 0 0 | 0 0 0 1 | 1 0 1 0 | 1 0 1 1 | 0 1
Sum(S) = A XOR B, and Carry(C) = A AND B. Although at circuit construction level usually NAND gates are used for implementation because all other gates can be constructed using just NANDs which reduces cost. 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: if A then if not B then ... else if B then ... end if
You will probably find many examples of code that are actualling using exclusive-or logic without using the operator directly.
|
|
|
Logged
|
esus saves.... Passes to Moses, shoots, he scores!
|
|
|
DrV
|
 |
« Reply #5 on: June 21, 2003, 12:49:09 AM » |
|
Swapping integers... a% = 3 b% = 4 a% = a% XOR b% b% = a% XOR b% a% = a% XOR b%
now a% = 4 and b% = 3
|
|
|
Logged
|
|
|
|
Moneo
|
 |
« Reply #6 on: June 21, 2003, 12:59:52 AM » |
|
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. flipflop = flipflop xor 1
Much simpler and no need to debug several if's. *****
|
|
|
Logged
|
|
|
|
Moneo
|
 |
« Reply #7 on: June 21, 2003, 01:24:52 AM » |
|
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: a xor b b xor a a xor b
Basically the same thing. *****
|
|
|
Logged
|
|
|
|
oracle
|
 |
« Reply #8 on: June 21, 2003, 01:27:16 AM » |
|
Edit Button! *dissolves into tears*
|
|
|
Logged
|
|
|
|
toonski84
|
 |
« Reply #9 on: June 21, 2003, 01:28:55 AM » |
|
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
|
|
|
Logged
|
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
|
|
|
oracle
|
 |
« Reply #10 on: June 21, 2003, 01:30:54 AM » |
|
Ooooooohh... maybe that's why no-one uses XOR :rotfl:
|
|
|
Logged
|
|
|
|
DrV
|
 |
« Reply #11 on: June 21, 2003, 01:39:28 AM » |
|
Excellent. Your example is a classic. You must be an assembly language programmer. ... Hehe... actually, I read that in VBPJ a looong time ago, and this thread made me remember it, so I went digging around for about 20 minutes and finally found it. 
|
|
|
Logged
|
|
|
|
Moneo
|
 |
« Reply #12 on: June 21, 2003, 02:03:22 AM » |
|
Ok, I'll take your word for it. the n=1-n may be faster than the n=n xor 1, but it's not as elegant. Plus, since I learned this trick in assembler, you coudn't do the n=1-n in one instruction in most assemblers that i've seen. But nevertheless, you win for now. *****
|
|
|
Logged
|
|
|
|
toonski84
|
 |
« Reply #13 on: June 21, 2003, 02:42:53 AM » |
|
yeah, it's more elegant. it also takes a 10th of a second longer to do 10 million operations. remember: when you're doing 10,000,000 switches, you're going to need all the speed you can get!
|
|
|
Logged
|
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
|
|
|
Antoni Gual
|
 |
« Reply #14 on: June 21, 2003, 05:23:46 AM » |
|
XOR? Mmmh.. 1.- I use it often to create a cheap texture screen 13 for i=0 to 63 for j=0 to 63 pset(i,j),i xor j next j,i
Edited: Nr 2 did'nt worked... 3.- The CRC algorithm is based on XOR (Code ripped from ABC) CRC = 0 'Reset for Each Text Block FOR I = 1 TO LEN(B$) 'Calculate for Length of Block ByteVal = ASC(MID$(B$, I, 1)) FOR J = 7 TO 0 STEP -1 TestBit = ((CRC AND 32768) = 32768) XOR ((ByteVal AND Power(J)) = Power(J)) CRC = ((CRC AND 32767&) * 2&) IF TestBit THEN CRC = CRC XOR &H1021& ' <-- This for 16 Bit CRC NEXT J NEXT I CRC16& = CRC
4.- The sprite animation technique often suggested to newbies: Excuse me if it's not exactly like this, you never find those darn snippets when you need them... PUT (x,y), mask(0), XOR PUT(X,Y),SPRITE(0),PSET
edited: BTW: Have you ever used EQV ? Not me....
|
|
|
Logged
|
Antoni
|
|
|
|