05-15-2005, 07:26 AM
I believe the following will rotate QB signed 16-bit integers 2 to the left. Note: this would be simpler if qb had unsigned data types.
Not the most elegant solution, but...
Code:
DEFINT A-Z
CLS
DO
INPUT "gimme a number"; a
PRINT a; " (dec) == "; HEX$(a); " (hex)"
SignBit = a < 0 ' true if 16th bit is on
'PRINT SignBit
nextBit = ((a AND &H4000) = &H4000) ' true if 15th bit is on
'PRINT nextBit
newSignBit = ((a AND &H2000) = &H2000) ' true if 14th bit is on
'PRINT newSignBit
a = a AND &H1FFF '&H1fff == 0001 1111 1111 1111...clear the bits that will fall off before shifting to prevent overflow errors
a = a * 4 ' shift left 2 bits
a = a + (-2 * SignBit) 'add bit to 2's place
a = a + (-1 * nextBit) 'add bit to 1's place
IF newSignBit THEN a = a OR &H8000 'add sign if needed
PRINT "your number rotated left 2 bits is"
PRINT a; " (dec) == "; HEX$(a); " (hex)"
LOOP
Not the most elegant solution, but...