05-30-2005, 08:03 PM
Quote:So, when rotating a positive number, is it possible for the number tobecome negative?
it should be if one is considering 16-bit signed integers. My original solution (post 5 of this thread) works fine.
Quote:So, when rotating a positive number, is it possible for the number tobecome negative?
Quote:So, when rotating a positive number, is it possible for the number tobecome negative?
Quote:Mine works fine too ^_^
DEFINT A-Z
CLS
t! = TIMER
FOR i = -32768 TO 32766
a = i
'DO
'INPUT "gimme a number"; a
'PRINT a; " (dec) == "; HEX$(a); " (hex)"
SignBit = a < 0
nextBit = ((a AND &H4000) = &H4000)
newSignBit = ((a AND &H2000) = &H2000)
a = a AND &H1FFF 'clear the bits that will fall off anyway-prevent overflow
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
NEXT i
PRINT TIMER - t!
CLS
t! = TIMER
FOR i% = -32768 TO 32766
valu% = i%
t$ = HEX$(valu%)
t$ = STRING$(4 - LEN(t$), 48) + t$
FOR a = 1 TO 4
p$ = MID$(t$, a, 1)
IF p$ = "0" THEN n$ = n$ + "0000"
IF p$ = "1" THEN n$ = n$ + "0001"
IF p$ = "2" THEN n$ = n$ + "0010"
IF p$ = "3" THEN n$ = n$ + "0011"
IF p$ = "4" THEN n$ = n$ + "0100"
IF p$ = "5" THEN n$ = n$ + "0101"
IF p$ = "6" THEN n$ = n$ + "0110"
IF p$ = "7" THEN n$ = n$ + "0111"
IF p$ = "8" THEN n$ = n$ + "1000"
IF p$ = "9" THEN n$ = n$ + "1001"
IF p$ = "A" THEN n$ = n$ + "1010"
IF p$ = "B" THEN n$ = n$ + "1011"
IF p$ = "C" THEN n$ = n$ + "1100"
IF p$ = "D" THEN n$ = n$ + "1101"
IF p$ = "E" THEN n$ = n$ + "1110"
IF p$ = "F" THEN n$ = n$ + "1111"
NEXT
rot$ = RIGHT$(n$, 14) + LEFT$(n$, 2)
n$ = ""
FOR a = 0 TO 3
p$ = MID$(rot$, 1 + a * 4, 4)
IF p$ = "0000" THEN n$ = n$ + "0"
IF p$ = "0001" THEN n$ = n$ + "1"
IF p$ = "0010" THEN n$ = n$ + "2"
IF p$ = "0011" THEN n$ = n$ + "3"
IF p$ = "0100" THEN n$ = n$ + "4"
IF p$ = "0101" THEN n$ = n$ + "5"
IF p$ = "0110" THEN n$ = n$ + "6"
IF p$ = "0111" THEN n$ = n$ + "7"
IF p$ = "1000" THEN n$ = n$ + "8"
IF p$ = "1001" THEN n$ = n$ + "9"
IF p$ = "1010" THEN n$ = n$ + "A"
IF p$ = "1011" THEN n$ = n$ + "B"
IF p$ = "1100" THEN n$ = n$ + "C"
IF p$ = "1101" THEN n$ = n$ + "D"
IF p$ = "1110" THEN n$ = n$ + "E"
IF p$ = "1111" THEN n$ = n$ + "F"
NEXT
num% = VAL("&H" + n$)
'PRINT num%
NEXT i%
PRINT TIMER - t!
DEFLNG Q
FOR Q = 0 TO 65535
Q1 = Q AND &HC000
QOUT = (Q - Q1) * 4 + Q1 / 16384
PRINT "input"; Q; "output"; QOUT
NEXT Q
Quote:this one (Mango's method) completes in less than 1/10 of a second on my machine
Quote:I just had to try itYour solution cuts several corners. The challenge requirements are for 16 bit integer numbers. This then makes the test range from
Code:DEFLNG Q
FOR Q = 0 TO 65535
Q1 = Q AND &HC000
QOUT = (Q - Q1) * 4 + Q1 / 16384
PRINT "input"; Q; "output"; QOUT
NEXT Q