Answer the question
In order to leave comments, you need to log in
The fastest algorithm dec -> bin in Pascal?
Tell me, please, what is the fastest algorithm for converting a number to binary from decimal can be implemented in Pascal?
There are already options:
1. Recursion:
function bin(a: integer):integer;<br>
begin<br>
if a>=2 then<br>
bin(a div 2);<br>
write(a mod 2);<br>
end;
while n>0 do begin<br>
if n mod 2 = 0 then<br>
s:=s+'0'<br>
else begin<br>
s:=s+'1';<br>
n:=n-1;<br>
end;<br>
n:=n div 2;<br>
end;<br>
for i:=length(s) downto 1 do<br>
write(s[i]);<br>
end;
while a>=1 do<br>
begin<br>
i:=i+1;<br>
b[i]:=a mod 2;<br>
a:=a div 2;<br>
end;<br>
n:=i;<br>
for i:=n downto 1 do<br>
write(b[i]);<br>
Answer the question
In order to leave comments, you need to log in
Obvious optimization:
b[i]:=a and 1;
a:=a shr 1;
Although the compiler has to guess.
well, if memory consumption is not important, then the table will be the fastest :)
Where are the decimals? There are machine numbers, they are already binary.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question