angryrabbit
-
Total Posts
:
36
- Scores: 2
-
Reward points
:
0
- Joined: 11/23/2010
-
Status: offline
|
Bit shift, bit rotate, xor, mod, or, not, and
Tuesday, December 07, 2010 5:05 AM
( permalink)
Functions I wrote while learning about SHA hashes. Mod, Xor, Or, Not, And overflows on numbers larger than 30 bits so I had to write different implementations of them. There is no native function for bit shifting and bit rotations so I had to create functions for those. Function largeOr(value, orValue, totalBits)
Dim i, a, b
largeOr = 0
For i = 0 To (totalBits - 1)
a = (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2))
b = (int((orValue / (2 ^ (i + 1)) - int(orValue / (2 ^ (i + 1)))) * 2))
If a Or b Then
largeOr = largeOr + 2 ^ i
End If
Next
End Function Function largeNot(value, totalBits)
Dim i, a
largeNot = 0
For i = 0 To (totalBits - 1)
a = int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)
If a = 0 Then
largeNot = largeNot + 2 ^ i
End If
Next
End Function Function largeAnd(value, andValue, totalBits)
Dim i, a, b
largeAnd = 0
For i = 0 To (totalBits - 1)
a = int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)
b = (int((andValue / (2 ^ (i + 1)) - int(andValue / (2 ^ (i + 1)))) * 2))
If a = 1 And b = 1 Then
largeAnd = largeAnd + 2 ^ i
End If
Next
End Function Function largeMod(value, amount)
largeMod = Int((Value / amount - int(value / amount)) * amount)
End Function Function rightRotate(value, amount, totalBits)
'To leftRotate, make amount = totalBits - amount
Dim i
rightRotate = 0
For i = 0 To (totalBits - 1)
If i >= amount Then
rightRotate = rightRotate + (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)) * 2 ^ (i - amount)
Else
rightRotate = rightRotate + (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)) * 2 ^ (totalBits - amount + i)
End If
Next
End Function Function largeXor(value, xorValue, totalBits)
Dim i, a, b
largeXor = 0
For i = 0 To (totalBits - 1)
a = (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2))
b = (int((xorValue / (2 ^ (i + 1)) - int(xorValue / (2 ^ (i + 1)))) * 2))
If a <> b Then
largeXor = largeXor + 2 ^ i
End If
Next
End Function Function leftShift(value, amount)
leftShift = value * 2 ^ amount
End Function Function rightShift(value, amount)
rightShift = Int(value / (2 ^ amount))
End Function
<message edited by angryrabbit on Tuesday, December 07, 2010 6:32 AM>
|
|
|
|