DCD plus!

I want to convert a the number 0 to 8 to the equivalent number of bits as efficiently as possible

e.g
3 -> 00000111
5 -> 00011111

Sort of like the DCD command but with bit filling

The obvious way is to shift in a bit in a loop

b0 = 6 ; (for example)
do while b0 > 0
b1 = b1 * 2 + 1
dec b0
loop

Does anyone know a whizzo quick way to do this please?
 

matherp

Senior Member
lookup b0,(%00000000,%00000001,%00000011,%00000111,%00001111,%00011111,%00111111,%01111111,%11111111),b1
 

nick12ab

Senior Member
ooh, I thought lookup was inefficient (slow)? Am I wrong?
Both ways use the same amount of program memory. However, just by moving the 'until' part, you can shave two bytes off the usage:
Code:
do
        b1 = b1 * 2 + 1
        dec b0
loop while b0 > 0
If you are using a PICAXE which has table functionality but you're not using it then you can also use that. You only need to use the readtable part repeatedly, use the table command once.
Code:
table (%1,%11,%111,%1111,%11111,%111111,%1111111,%11111111)
readtable b0,b1
 

matherp

Senior Member
Can't see why it would be. From a code perspective you are just adding b0 to the start address of a table and reading out the value held. You could also use:

table 0,(%00000000,%00000001,%00000011,%00000111,%00001111,%00011111,%00111111,%01111111,%11111111)
readtable b0,b1

which should be logically the same
 
Thanks folks. I just did some tests. lookup is 4-5 times faster than my original code and readtable is twice as fast as lookup.

Brilliant, thanks for your help, both.
 
Top