Qbasicnews.com

Full Version: Challenge: Identify ways to use XOR logical operator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Ooooooohh... maybe that's why no-one uses XOR :rotfl:
Quote: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. Smile
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.
*****
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!
XOR? Mmmh..

1.- I use it often to create a cheap texture
Code:
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)

Code:
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...

Code:
PUT (x,y), mask(0), XOR
PUT(X,Y),SPRITE(0),PSET

edited:

BTW: Have you ever used EQV ? Not me....
XOR is good, but it's a crappy name.

OR is good, AND is good. You can figure those out right off the bat.

But XOR? Come on men and women. Can't they make something cooler? Like.. "Addition of two bits without carry"
TO ANTONI:
Yes, the CRC calculation is probably one of the most popular uses of the XOR. The LRC (Longitudinal Redundancy Character or Check) used on some mainframe magnetic tape units, is a simpler computation that also uses the XOR, but is a less robust error checking technique than the CRC.

If anyone is interested, I have the "C" code for generating the identical CRC used by PKZIP. I didn't concieve it, but I fixed it to work.

x EQV y
EQV stands for Equivalent, meaning it tests if the expressions X and Y are the "same", i.e., either both true or both false.
I honestly have never had an opportunity to use it.
-----------------------------------------------------------------------------------
TO AGAMEMNUS:

Regarding the name of XOR, i.e., exclusive-or. The story that I heard years ago is that a regular OR is an inclusive-or. If either of the bits is on, then the resultant bit will be inclusively on. But, for an XOR, if the bits are both on, then the resultant bit will be exclusively off. OK, not the best definition. Note that in some languages an XOR is written as EOR.
*****
thats all too confusing for me...il just use my 300 line IF statements...at least they make sense to me
Quote: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

Err...
Code:
flipflop = !flipflop
Quote: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
b xor a
a xor b
Basically the same thing.
*****

I usually use XCHG. ;*)
Pages: 1 2 3