SRL Archive - OSR Intermediate Scripting Tutorials

Small xor tutorial.


The Un-Named

2007-08-29 08:55:44

Ok, not really super advanced, or useful for that matter, but it can easy fit in with a failsafe proc.

Ok, so lets begin, xor is a keyword, used pretty much the same as or. But, there's a difference, lets take the following script for example:

var
a : Integer;

begin
a := 5;
if ( a > 2) or
( a < 6 ) then
Writeln('a is either more than 2, less than 6, or even both!');
end.

Simple, if a is either more than 2, less than 6 or both, then it will write a string to the debug box. But lets say, we ONLY wanted to write to the debug box if only ONE condition was met? This is where xor comes into play.

var
a : Integer;

begin
a := 5;
if ( a > 2) xor
( a < 6 ) then
Writeln('a is either more than 2, or less than 6.');
end.

If you've been paying attention, you'll know that this doesn't print anything, change "( a < 6 ) then" to "( a < 4 ) then" and Bob's your uncle.

You can also mix it with the other operators such as and, or.

var
a : Integer;

begin
a := 5;
if ( a > 2) and
( a < 6 ) xor
not( a = 5 ) then
Writeln('if a is more than 2, and either less than 6, or not 5, then print!');
end.

Have fun. :P

Spky

2007-08-29 09:57:02

Nice. You should explain how it really works, though (returns 1 if values are different, 0 if the same).

0010100  xor
0100001 =
0110101
or like

Edit: NVM:p

Smartzkid

2007-08-29 10:10:22

xor = or not (in SCAR, atleast), right?

Nice tut, I'd almost forgotten about xor

The Un-Named

2007-08-29 11:16:30

From: Smartzkid
xor = or not (in SCAR, atleast), right?

Nice tut, I'd almost forgotten about xor


Exclusive or, I think.

From: Spky
Nice. You should explain how it really works, though (returns 1 if values are different, 0 if the same).

0010100  xor
0100001 =
0110101
or like

Edit: NVM:p


Forgot about that part of it. :o

Bebe

2007-08-29 11:26:44

This isn't really that advanced :P

I only used xor once when I was creating a password generator, though. But that was back on kaits.

solemn wishes

2007-08-29 15:28:46

sorry but i fail to see why this has been posted in the advancted section. Thankyou for sharing but this should definatly be in beginners, as many of the beginner tutorials show this information.

Wizzup?

2007-08-29 15:34:32

Exclusive Or, yes.

I only learnt what xor, shr and shl were after I got very annoyed by reading Ben's code who knew more 'bold programming thingies'. :p

The Un-Named

2007-08-29 17:40:38

From: solemn wishes
as many of the beginner tutorials show this information.


I only posted it because I've never saw anyone post it before. But yeah, I agree, it's not super advanced like you and me say, but I felt it'd be better here so people who probably could use it will.

There's only one place I think it could really be used, in the fishing guild to see if both docks are on the screen, (do a xor on searching for bitmaps or dtms of both docks) to determine if your in the bank if it's lets say, ridiculously crowded and no hopes of finding the symbol.

lordsaturn

2007-09-02 23:59:17

I just recently learned this concept in PHP. =]

ShowerThoughts

2007-09-03 08:01:25

its still a nice tut;)

ShawnjohnSJ

2007-09-03 08:25:34

Nice :]
I didnt even know xor was a command in SCAR.

The Un-Named

2007-09-03 19:59:22

From: ShawnjohnSJ
Nice :]
I didnt even know xor was a command in SCAR.



There's a lot more, but a lot of "un-used ones" that can't be used fully in SCAR, probably every one on this page is highlighted and bold in SCAR. You could even use some!

R0b0t1

2008-07-18 17:51:21

There are already binary operator tutorials, aren't there?


Oh, and theres also xnor, which: (not(op1 xor op2)). I've used it before. Returns true if not 1,0; 0,1.

Laur€ns

2008-07-23 09:59:08

What to say, gravedig? 09-04-2007.. That's almost a year.

Smartzkid

2008-07-23 21:28:51

So what? Its helpful information. A gravedig is only when someone spams an old thread to the top, IMO.

R0b0t1

2008-07-29 21:25:18

Oh, additionally, xor is almost (to my knowledge) the fastest computer operation.

n3ss3s

2008-08-05 03:05:55

(not(op1 xor op2)) == op1 and op2.

iseedcuk

2008-08-21 17:36:31

cool thank you

hutic

2008-11-16 20:47:15

nice tut

boberman

2008-11-17 01:44:43

From: R0b0t1
Oh, additionally, xor is almost (to my knowledge) the fastest computer operation.


mhh, not really, and, nand, or, nor will all be on the same order of speed. Last I read nand gates where the fastest. However for any single bit operations you are going to see about the same speed. The Inc operation is also pretty dang fast when you get down to the gate level of how it is implemented.

The thing is how fast a gate toggles really doesn't matter all that much. Every gate will operate within a clock cycle, so doing an bitwise and or a bitwise xor will show no difference at all. Clock cycles where introduced for good reasons, however their end result is that your computer can only operate at the speed of its slowest gate (Which, btw, they tend to use mostly nand gates as you can make all the other gates out of a couple of nand gates)

From: n3ss3s
(not(op1 xor op2)) == op1 and op2.


Wrong. The truth tables for each look like this

(xor)
op1 op2 answer
| 0 | 0 | 0
| 0 | 1 | 1
| 1 | 0 | 1
| 1 | 1 | 0


not (xor)
op1 op2 answer
| 0 | 0 | 1
| 0 | 1 | 0
| 1 | 0 | 0
| 1 | 1 | 1

and
op1 op2 answer
| 0 | 0 | 0
| 0 | 1 | 0
| 1 | 0 | 0
| 1 | 1 | 1

Clearly not xor is not equal to and

Tniffoc

2009-03-31 01:33:20

I use xor to confuse people :)

someNumberThatWillEqualZero := MSCX xor MSCX;

eruption144

2009-04-10 08:26:59

Thanks, I don't understand it much now, but it might help me later on. :)

XRaye

2009-04-26 03:30:00

Cool code, not really used much as far as i know though :p.

EDIT:

LOL the day after i read this i need it in a script :p.

marpis

2009-04-27 20:34:07

If you are very interested, i inspected xor with 3 booleans

OH didnt notice this was so old conversation. Well, i hope someone seems my kewl table :D i spent 5 valuable minutes doing it

EDIT: Btw, xor = ((a or b) and (not(a and b)))

Kasi

2009-04-27 21:25:22

gravedig…

Wizzup?

2009-04-27 22:07:59

From: Smartzkid
So what? Its helpful information. A gravedig is only when someone spams an old thread to the top, IMO.


…

Da 0wner

2009-04-29 05:56:28

Besides, there is not many threads to bump it over anyway.

ian.

2009-04-30 03:41:41

From: Wizzup?
…


D:< Is Wizzup? mad or agreeing? >.> I DON'T KNOW ANYTHING ANYMORE!

DAMN YOU!

:norris:

And marpis, I loved the table ;D