Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotate left 2 bits (like assembler)
#91
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.
Reply
#92
Mine works fine too ^_^
Reply
#93
Quote:So, when rotating a positive number, is it possible for the number tobecome negative?

If you're working with an integer, and you cause the leftmost bit (bit 15) to be set to a 1, the integer now becomes a negative number.

The leftmost bit of the integer can be set to a 1 by simulating a left shift operation, like multiplying by 2. If bit 14 was already a 1, then the result of multiplying by 2 (shiftting left 1 bit) will cause the 1 bit in bit 14 to move into bit 15, and thus the value of the integer becomes negative.

If you did the same operation above with a long integer, the result would have bit 15 set to 1, but since this a long integer, it won't become a negative number. However, if you transfer the resultant long integer into an integer, the integer will become negative.

So, any integer with bit 15 set to 1, is a negative number.
And, any long integer with bit 31 set to 1, is a negative number.

Lithium: Are you going to post the test program I asked for?
*****
Reply
#94
Quote:Mine works fine too ^_^

Works fine...I guess...if you consider a complicated solution that is over 100 x slower than my straight forward one working fine :roll:

this one (Mango's method) completes in less than 1/10 of a second on my machine
Code:
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!

While this one (Zire's method) completes in circa 10 seconds...
Code:
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!

your method defeats the whole point of rotate...it is supposed to be a low-cost operation...that's why it is available as an asm keyword...this business of converting to string and back using 32 if statements then using string member functions is over-the-top...
Reply
#95
I just had to try it
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
Reply
#96
Quote:this one (Mango's method) completes in less than 1/10 of a second on my machine

How long does the equivalent test take on your machine with my method? :-) (the DEF SEG/PEEK/POKE version)
Reply
#97
Quote:I just had to try it
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
Your solution cuts several corners. The challenge requirements are for 16 bit integer numbers. This then makes the test range from
-32768 to 32767. You used long integers and decided on your own range.

You can work with long integers within the logic if you like, but the input value and the output value for the stated range must be strictly an integer (16 bits).

In summary, your solution does not conform to the specifications.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)