Nice article... I like boolean maths... I think people need to use it more often... even for pointless stuff... so I wrote this (VB) code which will add two bytes together without using the standard(ish) operators... and VB doesn't have a bitshift, and while I was going to settle with doubling the number, I figured that recoding it without it would be much better :
(and I left out comments for a reason... I call it the "verbosity challenge" - do as little as possible with as much as possible, and obscure what it is doing while you are at it )
Public Function Add(ByVal Byte1 As Byte, ByVal Byte2 As Byte) As Byte
Dim Result As Byte
Dim Temp As Byte
Dim Temp2 As Byte
Result = Byte1
Temp = Byte2
Do While Temp
Temp2 = Result Xor Temp
Temp = Result And Temp
Result = Temp2
If (Temp And 128) = 128 Then
'Overflow
Error 6
End If
If (Temp And 64) = 64 Then
Temp = Temp Or 128
Else
Temp = Temp And (1 Or 2 Or 4 Or 8 Or 16 Or 32 Or 64)
End If
If (Temp And 32) = 32 Then
Temp = Temp Or 64
Else
Temp = Temp And (1 Or 2 Or 4 Or 8 Or 16 Or 32 Or 128)
End If
If (Temp And 16) = 16 Then
Temp = Temp Or 32
Else
Temp = Temp And (1 Or 2 Or 4 Or 8 Or 16 Or 64 Or 128)
End If
If (Temp And 8) = 8 Then
Temp = Temp Or 16
Else
Temp = Temp And (1 Or 2 Or 4 Or 8 Or 32 Or 64 Or 128)
End If
If (Temp And 4) = 4 Then
Temp = Temp Or 8
Else
Temp = Temp And (1 Or 2 Or 4 Or 16 Or 32 Or 64 Or 128)
End If
If (Temp And 2) = 2 Then
Temp = Temp Or 4
Else
Temp = Temp And (1 Or 2 Or 8 Or 16 Or 32 Or 64 Or 128)
End If
If (Temp And 1) = 1 Then
Temp = Temp Or 2
Else
Temp = Temp And (1 Or 4 Or 8 Or 16 Or 32 Or 64 Or 128)
End If
Temp = Temp And (2 Or 4 Or 8 Or 16 Or 32 Or 64 Or 128)
Loop
Add = Result
End Function
Trying is the first step towards failure.
Edited by - ragonastick on October 31, 2001 11:13:56 PM
|