Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fastest method for converting decimal to binary..
#1
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.
Reply
#2
Code:
INPUT a
DO
a1$ = LTRIM$(STR$(a MOD 2)) + a1$
a = INT(a / 2)
LOOP UNTIL a <= 0
PRINT a1$

remander method
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#3
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
Reply
#4
almost the same as mine
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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 Smile
Reply
#8
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.
Reply
#9
Give another example, so i can understand what are you expecting.
Reply
#10
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)