Qbasicnews.com
January 26, 2020, 09:16:02 AM
 Pages: 1 [2]
 Author Topic: Challenge: Identify ways to use XOR logical operator  (Read 7610 times)
Agamemnus
x/ \z

Posts: 3491

 « Reply #15 on: June 21, 2003, 10:44:29 AM »

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"
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an

Posts: 1971

 « Reply #16 on: June 21, 2003, 01:10:01 PM »

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.
*****
 Logged
HystericPoison
New Member

Posts: 23

 « Reply #17 on: June 21, 2003, 07:16:04 PM »

thats all too confusing for me...il just use my 300 line IF statements...at least they make sense to me
 Logged

This is the end of everything, you are the end of everything." -Slipknot - Everything Ends

"GOD HATES US ALL!!" -Slayer - God Hates Us All
LooseCaboose
I hold this place together

Posts: 981

 « Reply #18 on: June 22, 2003, 04:52:50 AM »

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
 Logged

esus saves.... Passes to Moses, shoots, he scores!
relsoft
*/-\*

Posts: 3927

 « Reply #19 on: June 22, 2003, 06:20:02 AM »

Quote from: "Moneo"
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. ;*)
 Logged

y smiley is 24 bit.

Genso's Junkyard:
http://rel.betterwebber.com/
whitetiger0990
__/--\__

Posts: 2964

 « Reply #20 on: June 22, 2003, 01:14:55 PM »

i think this
Code:
flop = -flop + 1

is easier then
Code:
flipflop = flipflop XOR 1

it certainly looks shorter
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Moneo
Na_th_an

Posts: 1971

 « Reply #21 on: June 22, 2003, 02:12:02 PM »

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
result = (a and not(b)) or (b and not(a))

*****
 Logged
DrV
Na_th_an

Posts: 1553

 « Reply #22 on: June 23, 2003, 01:30:02 AM »

Quote from: "Agamemnus"
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"

ummm... eXclusive OR?  one or the other but not both?
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #23 on: June 23, 2003, 01:33:12 PM »

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!
*****
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #24 on: June 23, 2003, 01:52:35 PM »

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
*****
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #25 on: July 03, 2003, 03:32:51 PM »

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.
DEFINT A-Z
FOR X=0 TO 255
GRAY = X XOR (INT(X/2))
rem Print or output the Gray Code value in GRAY.
NEXT X
 Logged
 Pages: 1 [2]