Posts: 3,368
Threads: 195
Joined: Jan 2003
What's the fastest way to do this..:?
input: bytestream of "0123456789"
output: integer array
input: integer array
output: bytestream of "0123456789"
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 2,765
Threads: 138
Joined: Nov 2002
Code:
INPUT a
DO
a1$ = LTRIM$(STR$(a MOD 2)) + a1$
a = INT(a / 2)
LOOP UNTIL a <= 0
PRINT a1$
remander method
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 2,020
Threads: 24
Joined: Jun 2002
Code:
INPUT a
a$ = ""
DO
if a and 1 then a$ = a$ + "1" else a$ = a$ + "0"
a = a \ 2
LOOP UNTIL a = 0
PRINT a$
a little bit faster. It's the fastest way I know anyways. I might have mixed up the 4th line though. Untested.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 2,765
Threads: 138
Joined: Nov 2002
almost the same as mine
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 90
Threads: 8
Joined: Aug 2003
I use the same basic algorithm, the idea to optimize is to use as fewest string operations, (concatenate strings is an expensive job).
Code:
input a%
if a% = 0 then
a$ = "0"
else
' in i% how many digits i need
i%=0
b% = a%
while b% > 0
i% = i% + 1
b% = b% \ 2
wend
' i make an string with "0"s
a$ = string$(i%, "0")
i% = 1
while a% > 0
' just replace the 1
if a% and 1 then
mid$(a$,i%,1) = "1"
endif
a% = a% \ 2
wend
endif
The code surely will not be very fast with small numbers.
Posts: 3,368
Threads: 195
Joined: Jan 2003
I want to use integer arrays...............................................................................................................................................................................................................................
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,845
Threads: 44
Joined: Aug 2002
Ok, let me try:
For one way:
Code:
number& = 6445
REDIM Array(LOG(number&) / LOG(2)) AS INTEGER
FOR I = 0 TO UBOUND(Array)
IF (2& ^ I ) AND number& THEN Array(I) = 1 ELSE Array(I) = 0
NEXT I
For the other:
Code:
'the array is called 'array'
FOR I = 0 TO UBOUND(Array)
IF Array(I) THEN nr& = nr& + (2& ^ I)
NEXT I
I hope this suits you
Posts: 3,368
Threads: 195
Joined: Jan 2003
No.... :|
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 90
Threads: 8
Joined: Aug 2003
Give another example, so i can understand what are you expecting.
Posts: 3,368
Threads: 195
Joined: Jan 2003
neo has the right idea, but he's using a long instead of a string.
Example:
"89865876578" becomes an integer array that uses as many bits as the minimum bits required to hold this string's number, 16 bits per integer. (although 15 would be OK...)
the integer array becomes "89865876578"
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.