Re: Sequence items to a single number ? How to achieve ?

new topic     » goto parent     » topic index » view thread      » older message » newer message

There's a small problem with your conversion routine (and also Selgor's). You are putting the bits into the array backwards. It doesn't show because 5 is palindromic in binary but if you try it with another number such as 13 you will see that the bits are reversed.

evanmars said...

try this

include std/math.e 
integer d = 5  --your decimal number 
 
sequence b = {} 
 
while 1 do 
    b = append(b,mod(d,2)) 
-- could either do  
-- b = prepend(b, mod(d,2)) -- or 
-- b = mod(d,2) & b 
    d = floor(d/2) 
    if d = 0 then 
        exit 
    end if 
end while 
 
puts(1,"{") 
for i = 1 to length(b) do 
    printf(1,"%d",b[i]) 
    if i != length(b) then 
        puts(1,",") 
    end if 
end for 
puts(1,"}") 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu