Works as long as both strings are the same length...
Code: Select all
screen 19
'====================================================
'====================================================
function Redditt_and( a as string , b as string ) as string
dim as longint x = len(b) - 1
do
b[x]+= a[x]
if b[x] = 98 then b[x] = 49 else b[x] = 48
x-=1
loop until x = -1
return b
end function
'====================================================
'====================================================
function Redditt_or( a as string , b as string ) as string
dim as longint x = len(b) - 1
do
b[x]+= a[x]
if b[x] = 96 then b[x] = 48 else b[x] = 49
x-=1
loop until x = -1
return b
end function
'====================================================
'====================================================
function Redditt_xor( a as string , b as string ) as string
dim as longint x = len(b) - 1
do
b[x]+= a[x]
if b[x] = 97 then b[x] = 49 else b[x] = 48
x-=1
loop until x = -1
return b
end function
'====================================================
'====================================================
'test functions
'====================================================
'====================================================
randomize
do
dim as ulongint a = int( rnd*1e10 )
dim as ulongint b = int( rnd*1e10 )
if b > a then swap a , b
dim as double time1 , time2
dim as double b_and_time
dim as double r_and_time
dim as double b_or_time
dim as double r_or_time
dim as double b_xor_time
dim as double r_xor_time
dim as ulongint b_and
dim as ulongint b_or
dim as ulongint b_xor
dim as ulongint r_and
dim as ulongint r_or
dim as ulongint r_xor
'======================================
'builtin AND
time1 = timer
'for x as longint = 1 to 1e6
b_and = ( a and b)
'next
time2 = timer
b_and_time = time2 - time1
'Redditt_AND
dim as string and_ans = ""
dim as string and_1 = right( string(64,"0") + bin(a) , 64 )
dim as string and_2 = right( string(64,"0") + bin(b) , 64 )
time1 = timer
'for x as longint = 1 to 1e6
and_ans = Redditt_and( and_1 , and_2 )
'next
r_and = valulng("&B" + and_ans)
time2 = timer
r_and_time = time2 - time1
'======================================
'======================================
'builtin OR
time1 = timer
'for x as longint = 1 to 1e6
b_or = ( a or b)
'next
time2 = timer
b_or_time = time2 - time1
'Redditt_OR
dim as string or_ans = ""
dim as string or_1 = right( string(64,"0") + bin(a) , 64 )
dim as string or_2 = right( string(64,"0") + bin(b) , 64 )
time1 = timer
'for x as longint = 1 to 1e6
or_ans = Redditt_or( or_1 , or_2 )
'next
r_or = valulng("&B" + or_ans)
time2 = timer
r_or_time = time2 - time1
'======================================
'builtin XOR
time1 = timer
'for x as longint = 1 to 1e6
b_xor = ( a xor b)
'next
time2 = timer
b_xor_time = time2 - time1
'Redditt_XOR
dim as string xor_ans = ""
dim as string xor_1 = right( string(64,"0") + bin(a) , 64 )
dim as string xor_2 = right( string(64,"0") + bin(b) , 64 )
time1 = timer
'for x as longint = 1 to 1e6
xor_ans = Redditt_xor( xor_1 , xor_2 )
'next
r_xor = valulng("&B" + xor_ans)
time2 = timer
r_xor_time = time2 - time1
'======================================
dim as longint and_diff = b_and - r_and
dim as longint or_diff = b_or - r_or
dim as longint xor_diff = b_xor - r_xor
cls
print
print "n1 = " ; a
print "n2 = " ; b
print
print "Builtin AND = " ; b_and , "time = " ; b_and_time
print "Redditt AND = " ; r_and , "time = " ; r_and_time
print
print "Builtin OR = " ; b_or , "time = " ; b_or_time
print "Redditt OR = " ; r_or , "time = " ; r_or_time
print
print "Builtin XOR = " ; b_xor , "time = " ; b_xor_time
print "Redditt XOR = " ; r_xor , "time = " ; r_xor_time
print "AND diff = "; and_diff
print "OR diff = "; or_diff
print "XOR diff = "; xor_diff
print
print "Press a key to advance" , "press esc to exit"
sleep
if inkey = chr(27) then end
loop until inkey = chr(27)
end