05-19-2005, 11:01 PM
Well well, qb has once again proven to me how much it really sucks. And real compiler should convert everything in an expression to the largest datatype, especially constants. Thus & wouldn't be needed in an expression where you are using longs. But looks like qb doesn't do that. So here you go, this one works
Longs are needed becuase otherwise the IDE will report an overflow in many cases. When compiled they aren't needed. I tested all the cases you talked about, all work fine as you'll see here
16384
0100000000000000
0000000000000001
0000000000000100
0000000000010000
0000000001000000
0000000100000000
0000010000000000
0001000000000000
-1
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
-2
1111111111111110
1111111111111011
1111111111101111
1111111110111111
1111111011111111
1111101111111111
1110111111111111
1011111111111111
1111111111111110
Tested with
How does it work? Simple, get the top 2 bits and move them down 14 bits, shift the number 2 bits to the left and combine it with the result of the previous operation. That's what bit rotating is. No branches needed, it's all simple logic.
Code:
function rotLeft& ( num as long )
rotLeft& = ((num * 4) and &hffff&) or (num and &hc000&)\&h4000&
end function
Longs are needed becuase otherwise the IDE will report an overflow in many cases. When compiled they aren't needed. I tested all the cases you talked about, all work fine as you'll see here
16384
0100000000000000
0000000000000001
0000000000000100
0000000000010000
0000000001000000
0000000100000000
0000010000000000
0001000000000000
-1
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
-2
1111111111111110
1111111111111011
1111111111101111
1111111110111111
1111111011111111
1111101111111111
1110111111111111
1011111111111111
1111111111111110
Tested with
Code:
cls
test& = somenumber.....
for i = 0 to 8
printBin test
test = rotLeft&( test )
next i
How does it work? Simple, get the top 2 bits and move them down 14 bits, shift the number 2 bits to the left and combine it with the result of the previous operation. That's what bit rotating is. No branches needed, it's all simple logic.
Code:
shl 2 |1100010100000010|
-------------------
11|0001010000001000|
-------------------
|1100010100000010|
and |1100000000000000|
-------------------
shr 14 |1100000000000000|
-------------------
|0000000000000011|00000000000000
-------------------
or |0001010000001000|
|0000000000000011|
-------------------
|0001010000001011|
There, done
oship me and i will give you lots of guurrls and beeea