Page 1 of 1 1
Topic Options
#83499 - 02/09/13 04:33 PM XOR() - Exclusive OR operation using bitwise AND
Bonji Offline
Starting to like KiXtart

Registered: 01/09/28
Posts: 148
Loc: Virginia
code:
;Function	XOR()
;
;Author Ben Dulaney
;
;Version 1.0
;
;Action Performs an eXclusive OR operation on a binary value
;
;Syntax XOR("Binary Value", "Bit to Swap")
;
;Parameters
; BINARY VALUE : REQUIRED. The value of the binary number to be manipulated.
; BIT TO SWAP : REQUIRED. A Power of 2 Number (i.e. 2,4,8,16,32,512)
;
;Returns Value of bit-swapped binary number
; 1 if BIT TO SWAP is invalid
;
;Dependencies KiX 4.10+
;
;Example $VAL1 = XOR(577,64)
;
;Source

Function XOR($BinaryVal,$Bit)
Dim $TstBit
$TstBit = $Bit
WHILE $TstBit MOD 2 = 0 AND $TstBit <> 1
$TstBit = $TstBit / 2
LOOP
IF $TstBit <> 1
EXIT(1)
RETURN
ENDIF
Dim $TmpVar
$TmpVar=$BinaryVal - ($BinaryVal & $Bit)
IF $TmpVar = $BinaryVal ;The bit was off
$XOR = $BinaryVal + $Bit
ELSE ;The bit was on
$XOR = $BinaryVal - $Bit
ENDIF
EndFunction



[ 13. September 2002, 19:59: Message edited by: Ben Dulaney ]

Top
#83500 - 02/09/13 05:40 PM Re: XOR() - Exclusive OR operation using bitwise AND
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 01/06/05
Posts: 20439
Loc: FI
ok, according to change logs, the first version supporting mod is 4.10
 

Top
#83501 - 02/10/22 06:30 AM Re: XOR() - Exclusive OR operation using bitwise AND
Paul Lemaire Offline
Lurker

Registered: 01/12/06
Posts: 2
Loc: Milford, NH
This can be reduced to a one-liner. The following version works on machines that use twos-complement arithmetic (PCs). Instead of a single bit, you can supply a mask. Also, the arguments are interchangeable.

Function XOR($BinaryVal, $Mask)

$XOR = ($BinaryVal | $Mask) & (-1 - ($BinaryVal & $Mask))

EndFunction
_________________________
Paul Lemaire Ace Consulting Reality is stranger than fiction.

Top
#83502 - 02/10/30 06:12 AM Re: XOR() - Exclusive OR operation using bitwise AND
Anonymous
Unregistered


http://www.scriptlogic.com/Kixtart/ViewFunction.asp?FN=XOR
Top
Page 1 of 1 1


Moderator:  Jochen, Radimus, Sealeopard, Bryce, Kdyer, Howard Bullock, Chris S., Glenn Barnas, Allen, Benny69, Mart 
Hop to:
Shout Box

Who's Online
5 registered (Digi, Mart, It_took_my_meds, Glenn Barnas, Sam_B) and 69 anonymous users online.

Generated in 2.235 seconds in which 0.646 seconds were spent on a total of 11 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org