@Richard
I fiddled around and got timings for your two sets of logic functions..
( Richard Logic 1 )
Code: Select all
'
' Bitwise Logic Functions, NOT, AND, OR, XOR, by arithmetic without using those logic functions
'-------------------------------------------------------
Function arith_NOT( Byval x As ulongint ) As ulongint
Return - x-1
End Function
'-------------------------------------------------------
Function arith_AND( Byval x As ulongint , Byval y As ulongint ) As ulongint
Dim As ulongint temp = 0, pit = 1
For i As Integer = 0 To 63
If x <> ( x Shr 1 ) Shl 1 Then
If y <> ( y Shr 1 ) Shl 1 Then temp = temp + pit
End If
pit = pit + pit
x = x Shr 1
y = y Shr 1
Next i
Return temp
End Function
'-------------------------------------------------------
Function arith_OR( Byval x As ulongint , Byval y As ulongint ) As ulongint
Dim As ulongint temp = 0, pit = 1, flag
For i As Integer = 0 To 63
If x <> ( x Shr 1 ) Shl 1 Then flag = 1
If y <> ( y Shr 1 ) Shl 1 Then flag = 1
If flag Then
temp = temp + pit
flag = 0
End If
pit = pit + pit
x = x Shr 1
y = y Shr 1
Next i
Return temp
End Function
'-------------------------------------------------------
Function arith_XOR( Byval x As ulongint , Byval y As ulongint ) As ulongint
Dim As ulongint temp = 0, pit = 1, flag = 0
For i As Integer = 0 To 63
If x <> ( x Shr 1 ) Shl 1 Then flag = 1
If y <> ( y Shr 1 ) Shl 1 Then flag = 1 - flag
If flag Then
temp = temp + pit
flag = 0
End If
pit = pit + pit
x = x Shr 1
y = y Shr 1
Next i
Return temp
End Function
'================================================================
'================================================================
'test functions
'================================================================
'================================================================
screen 19
randomize
dim as double time1 , time2
dim as double b_not , r_not
dim as double b_and , r_and
dim as double b_or , r_or
dim as double b_xor , r_xor
do
dim as ulongint n1 = int( rnd*1e10)
dim as ulongint n2 = int( rnd*1e10)
dim as ulongint ans
cls
'built in functions , time for loops 1e6
time1 = timer
for x as longint = 1 to 1e6
ans = not n1
next
time2 = timer
b_not = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = n1 and n2
next
time2 = timer
b_and = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = n1 or n2
next
time2 = timer
b_or = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = n1 xor n2
next
time2 = timer
b_xor = time2 - time1
'Richrd functions , time for loops 1e6
time1 = timer
for x as longint = 1 to 1e6
ans = arith_not( n1 )
next
time2 = timer
r_not = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = arith_and( n1 , n2)
next
time2 = timer
r_and = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = arith_or( n1 , n2)
next
time2 = timer
r_or = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = arith_xor( n1 , n2 )
next
time2 = timer
r_xor = time2 - time1
print "================================================="
print "Builtin NOT time , 1e6 loops = " ; b_not
print "Richard NOT time , 1e6 loops = " ; r_not
print
print "Builtin AND time , 1e6 loops = " ; b_and
print "Richard AND time , 1e6 loops = " ; r_and
print
print "Builtin OR time , 1e6 loops = " ; b_or
print "Richard OR time , 1e6 loops = " ; r_or
print
print "Builtin XOR time , 1e6 loops = " ; b_xor
print "Richard XOR time , 1e6 loops = " ; r_xor
print "================================================="
print
print "n1 = " ; n1
print "n2 = " ; n2
print
print "Builtin NOT = "; not n1
print "Richard NOT = " ; arith_NOT( n1 )
print
print "Builtin AND = " ; n1 and n2
print "Richard AND = "; arith_AND( n1 , n2 )
print
print "Builtin OR = " ; n1 or n2
print "Richard OR = " ; arith_OR( n1 , n2)
print
print "Builtin XOR = " ; n1 xor n2
print "Richard XOR = "; arith_XOR(n1 , n2)
sleep
loop until inkey = chr(27)
sleep
end
( Richard Logic 2 )
Code: Select all
'=======================================================================
' Backward logic, boolean from arithmetic
'=======================================================================
' "I'm not trying to write all the functions , just the logic functions" ... Albert
' Rules; for the logic functions , you can use ( add , sub , mul , div , shr , shl , mod )
'=======================================================================
#Define Ltype ulongint ' identify the data type used for bitwise logical variables
'-------------------------------------------------------
' the odd arithmetic helper function
' Function odd( Byval x As Ltype ) As Ltype
' Return x - 2 * ( x \ 2 ) ' do not optimise by cancellation of 2
' End Function
#Define odd( x ) ( (x) - 2 * ( (x) \ 2 ) )
'-------------------------------------------------------
Function arith_NOT( Byval x As Ltype ) As Ltype
Return &hFFFFFFFFFFFFFFFFull - x
End Function
'-------------------------------------------------------
Function arith_AND( Byval x As Ltype, Byval y As Ltype ) As Ltype
Dim As Ltype t = 0, z = 1
Do
t += z * odd( x ) * odd( y )
x \= 2
y \= 2
z *= 2
Loop While z
Return t
End Function
'-------------------------------------------------------
Function arith_OR( Byval x As Ltype, Byval y As Ltype ) As Ltype
Dim As Ltype t = 0, z = 1
Do
t += z * ( 1 - ( ( 1 - odd( x ) ) * ( 1 - odd( y ) ) ) )
x \= 2
y \= 2
z *= 2
Loop While z
Return t
End Function
'-------------------------------------------------------
Function arith_XOR( Byval x As Ltype, Byval y As Ltype ) As Ltype
Dim As Ltype t = 0, z = 1
Do
t += z * odd( odd( x ) + odd( y ) )
x \= 2
y \= 2
z *= 2
Loop While z
Return t
End Function
'-------------------------------------------------------
'================================================================
'================================================================
'test functions
'================================================================
'================================================================
screen 19
randomize
dim as double time1 , time2
dim as double b_not , r_not
dim as double b_and , r_and
dim as double b_or , r_or
dim as double b_xor , r_xor
do
dim as ulongint n1 = int( rnd*1e10)
dim as ulongint n2 = int( rnd*1e10)
dim as ulongint ans
cls
'built in functions , time for loops 1e6
time1 = timer
for x as longint = 1 to 1e6
ans = not n1
next
time2 = timer
b_not = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = n1 and n2
next
time2 = timer
b_and = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = n1 or n2
next
time2 = timer
b_or = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = n1 xor n2
next
time2 = timer
b_xor = time2 - time1
'Richrd functions , time for loops 1e6
time1 = timer
for x as longint = 1 to 1e6
ans = arith_not( n1 )
next
time2 = timer
r_not = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = arith_and( n1 , n2)
next
time2 = timer
r_and = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = arith_or( n1 , n2)
next
time2 = timer
r_or = time2 - time1
time1 = timer
for x as longint = 1 to 1e6
ans = arith_xor( n1 , n2 )
next
time2 = timer
r_xor = time2 - time1
print "================================================="
print "Builtin NOT time , 1e6 loops = " ; b_not
print "Richard NOT time , 1e6 loops = " ; r_not
print
print "Builtin AND time , 1e6 loops = " ; b_and
print "Richard AND time , 1e6 loops = " ; r_and
print
print "Builtin OR time , 1e6 loops = " ; b_or
print "Richard OR time , 1e6 loops = " ; r_or
print
print "Builtin XOR time , 1e6 loops = " ; b_xor
print "Richard XOR time , 1e6 loops = " ; r_xor
print "================================================="
print
print "n1 = " ; n1
print "n2 = " ; n2
print
print "Builtin NOT = "; not n1
print "Richard NOT = " ; arith_NOT( n1 )
print
print "Builtin AND = " ; n1 and n2
print "Richard AND = "; arith_AND( n1 , n2 )
print
print "Builtin OR = " ; n1 or n2
print "Richard OR = " ; arith_OR( n1 , n2)
print
print "Builtin XOR = " ; n1 xor n2
print "Richard XOR = "; arith_XOR(n1 , n2)
sleep
loop until inkey = chr(27)
sleep
end
On my Linux System
With Geany IDE , build command fbc.exe -gen GCC -W all - O 3 , they both , come out to the same times as the builtin functions..
6.000?? e-007
I have an AMD quad core 2GHz processor with , OS = Ubuntu Linux 18.04 LTS 64 bit..