Binary digits: Difference between revisions

→‎{{header|AppleScript}}: Added two straightforward solutions.
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
(→‎{{header|AppleScript}}: Added two straightforward solutions.)
Line 625:
50 -> 一一〇〇一〇
9000 -> 一〇〇〇一一〇〇一〇一〇〇〇</pre>
 
=== Straightforward ===
 
At its very simplest, an AppleScript solution would look something like this:
 
<lang applescript>on intToBinary(n)
set binary to (n mod 2 div 1) as text
set n to n div 2
repeat while (n > 0)
set binary to ((n mod 2 div 1) as text) & binary
set n to n div 2
end repeat
return binary
end intToBinary
 
display dialog ¬
intToBinary(5) & linefeed & ¬
intToBinary(50) & linefeed & ¬
intToBinary(9000) & linefeed</lang>
 
Building a list of single-digit values instead and coercing that at the end can be a tad faster, but execution can be four or five times as fast when groups of text (or list) operations are replaced with arithmetic:
 
<lang applescript>on intToBinary(n)
set binary to ""
repeat
-- Calculate an integer value whose 8 decimal digits are the same as the low 8 binary digits of n's current value.
set binAsDec to (n div 128 mod 2 * 10000000 + n div 64 mod 2 * 1000000 + n div 32 mod 2 * 100000 + ¬
n div 16 mod 2 * 10000 + n div 8 mod 2 * 1000 + n div 4 mod 2 * 100 + n div 2 mod 2 * 10 + n mod 2) div 1
-- Coerce to text as appropriate, prepend to the output text, and prepare to get another 8 digits or not as necessary.
if (n > 255) then
set binary to text 2 thru -1 of ((100000000 + binAsDec) as text) & binary
set n to n div 256
else
set binary to (binAsDec as text) & binary
exit repeat
end if
end repeat
return binary
end intToBinary
 
display dialog ¬
intToBinary(5) & linefeed & ¬
intToBinary(50) & linefeed & ¬
intToBinary(9000) & linefeed</lang>
 
=={{header|ARM Assembly}}==
557

edits