Binary digits: Difference between revisions

→‎UNIX Shell: replace bc wrapper (already on page) with native implementation
(→‎UNIX Shell: replace bc wrapper (already on page) with native implementation)
Line 5,505:
 
0 OK, 0:775</pre>
 
=={{header|UNIX Shell}}==
Since POSIX does not specify local variables, make use of <code>set</code> for highest portability.
<syntaxhighlight lang="sh"># Define a function to output binary digits
<syntaxhighlight lang="sh">bin() {
tobinary() {
set -- "${1:-0}" ""
# We use the bench calculator for our conversion
echo while [ 1 -lt "obase=2;$1"|bc ]
do
set -- $(($1 >> 1)) $(($1 & 1))$2
done
echo "$1$2"
}
 
echo $(for i in 0 1 2 5 50 9000; do bin $i; done)</syntaxhighlight>
# Call the function with each of our values
{{out}}
tobinary 5
<pre>0 1 10 101 110010 10001100101000</pre>
tobinary 50</syntaxhighlight>
 
=={{header|VBA}}==
'''2 ways :'''
559

edits