Binary digits: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 1: | Line 1: | ||
{{task|Basic language learning}} |
|||
[[Category:Radices]] |
[[Category:Radices]] |
||
{{task|Basic language learning}} |
|||
;Task: |
;Task: |
||
Line 15: | Line 15: | ||
There should be no other whitespace, radix or sign markers in the produced output, and [[wp:Leading zero|leading zeros]] should not appear in the results. |
There should be no other whitespace, radix or sign markers in the produced output, and [[wp:Leading zero|leading zeros]] should not appear in the results. |
||
<br><br> |
<br><br> |
||
=={{header|8th}}== |
|||
<syntaxhighlight lang="forth"> |
|||
=={{header|0815}}== |
|||
2 base drop |
|||
<syntaxhighlight lang=0815>}:r:|~ Read numbers in a loop. |
|||
#50 . cr |
|||
}:b: Treat the queue as a stack and |
|||
</syntaxhighlight> |
|||
<:2:= accumulate the binary digits |
|||
/=>&~ of the given number. |
|||
^:b: |
|||
<:0:-> Enqueue negative 1 as a sentinel. |
|||
{ Dequeue the first binary digit. |
|||
}:p: |
|||
~%={+ Rotate each binary digit into place and print it. |
|||
^:p: |
|||
<:a:~$ Output a newline. |
|||
^:r:</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
|||
Note that 0815 reads numeric input in hexadecimal. |
|||
<syntaxhighlight lang=bash>echo -e "5\n32\n2329" | 0815 bin.0 |
|||
101 |
|||
110010 |
110010 |
||
</pre> |
|||
10001100101001</syntaxhighlight> |
|||
=={{header|11l}}== |
=={{header|11l}}== |
||
<syntaxhighlight lang=11l>L(n) [0, 5, 50, 9000] |
<syntaxhighlight lang="11l">L(n) [0, 5, 50, 9000] |
||
print(‘#4 = #.’.format(n, bin(n)))</syntaxhighlight> |
print(‘#4 = #.’.format(n, bin(n)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 49: | Line 34: | ||
9000 = 10001100101000 |
9000 = 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
<syntaxhighlight lang=360asm>* Binary digits 27/08/2015 |
<syntaxhighlight lang="360asm">* Binary digits 27/08/2015 |
||
BINARY CSECT |
BINARY CSECT |
||
USING BINARY,R12 |
USING BINARY,R12 |
||
Line 101: | Line 85: | ||
9000 10001100101000 |
9000 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|0815}}== |
|||
<syntaxhighlight lang="0815">}:r:|~ Read numbers in a loop. |
|||
}:b: Treat the queue as a stack and |
|||
<:2:= accumulate the binary digits |
|||
/=>&~ of the given number. |
|||
^:b: |
|||
<:0:-> Enqueue negative 1 as a sentinel. |
|||
{ Dequeue the first binary digit. |
|||
}:p: |
|||
~%={+ Rotate each binary digit into place and print it. |
|||
^:p: |
|||
<:a:~$ Output a newline. |
|||
^:r:</syntaxhighlight> |
|||
{{out}} |
|||
Note that 0815 reads numeric input in hexadecimal. |
|||
<syntaxhighlight lang="bash">echo -e "5\n32\n2329" | 0815 bin.0 |
|||
101 |
|||
110010 |
|||
10001100101001</syntaxhighlight> |
|||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
{{works with|http://vice-emu.sourceforge.net/ VICE}} |
{{works with|http://vice-emu.sourceforge.net/ VICE}} |
||
Line 122: | Line 127: | ||
strout = $cb1e |
strout = $cb1e |
||
</pre> |
</pre> |
||
<syntaxhighlight lang=6502asm> |
<syntaxhighlight lang="6502asm"> |
||
; C64 - Binary digits |
; C64 - Binary digits |
||
; http://rosettacode.org/wiki/Binary_digits |
; http://rosettacode.org/wiki/Binary_digits |
||
Line 228: | Line 233: | ||
100 |
100 |
||
</pre> |
</pre> |
||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
<syntaxhighlight lang=8080asm>bdos: equ 5h ; CP/M system call |
<syntaxhighlight lang="8080asm">bdos: equ 5h ; CP/M system call |
||
puts: equ 9h ; Print string |
puts: equ 9h ; Print string |
||
org 100h |
org 100h |
||
Line 270: | Line 274: | ||
</pre> |
</pre> |
||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
<syntaxhighlight lang=asm> .model small |
<syntaxhighlight lang="asm"> .model small |
||
.stack 1024 |
.stack 1024 |
||
.data |
.data |
||
Line 357: | Line 360: | ||
ret |
ret |
||
PrintBinary_NoLeadingZeroes endp</syntaxhighlight> |
PrintBinary_NoLeadingZeroes endp</syntaxhighlight> |
||
=={{header|8th}}== |
|||
<syntaxhighlight lang=forth> |
|||
2 base drop |
|||
#50 . cr |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
110010 |
|||
</pre> |
|||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="aarch64 assembly"> |
||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program binarydigit.s */ |
/* program binarydigit.s */ |
||
Line 532: | Line 524: | ||
The decimal value +1 should produce an output of 1 |
The decimal value +1 should produce an output of 1 |
||
</pre> |
</pre> |
||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lisp">(include-book "arithmetic-3/top" :dir :system) |
||
(defun bin-string-r (x) |
(defun bin-string-r (x) |
||
Line 549: | Line 540: | ||
"0" |
"0" |
||
(bin-string-r x)))</syntaxhighlight> |
(bin-string-r x)))</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="action!">PROC PrintBinary(CARD v) |
||
CHAR ARRAY a(16) |
CHAR ARRAY a(16) |
||
BYTE i=[0] |
BYTE i=[0] |
||
Line 590: | Line 580: | ||
Output for 9000 is 10001100101000 |
Output for 9000 is 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with ada.text_io; use ada.text_io; |
||
procedure binary is |
procedure binary is |
||
bit : array (0..1) of character := ('0','1'); |
bit : array (0..1) of character := ('0','1'); |
||
Line 613: | Line 602: | ||
Output for 9000 is 10001100101000 |
Output for 9000 is 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
<syntaxhighlight lang=aime>o_xinteger(2, 0); |
<syntaxhighlight lang="aime">o_xinteger(2, 0); |
||
o_byte('\n'); |
o_byte('\n'); |
||
o_xinteger(2, 5); |
o_xinteger(2, 5); |
||
Line 627: | Line 615: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68|Revision 1.}} |
{{works with|ALGOL 68|Revision 1.}} |
||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.3 algol68g-2.3.3].}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.3 algol68g-2.3.3].}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to use of '''format'''[ted] ''transput''.}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to use of '''format'''[ted] ''transput''.}} |
||
'''File: Binary_digits.a68'''<syntaxhighlight lang=algol68>#!/usr/local/bin/a68g --script # |
'''File: Binary_digits.a68'''<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script # |
||
printf(( |
printf(( |
||
Line 655: | Line 642: | ||
+9000 => TFFFTTFFTFTFFF |
+9000 => TFFFTTFFTFTFFF |
||
</pre> |
</pre> |
||
=={{header|ALGOL W}}== |
|||
<syntaxhighlight lang="algolw">begin |
|||
% prints an integer in binary - the number must be greater than zero % |
|||
procedure printBinaryDigits( integer value n ) ; |
|||
begin |
|||
if n not = 0 then begin |
|||
printBinaryDigits( n div 2 ); |
|||
writeon( if n rem 2 = 1 then "1" else "0" ) |
|||
end |
|||
end binaryDigits ; |
|||
% prints an integer in binary - the number must not be negative % |
|||
procedure printBinary( integer value n ) ; |
|||
begin |
|||
if n = 0 then writeon( "0" ) |
|||
else printBinaryDigits( n ) |
|||
end printBinary ; |
|||
% test the printBinaryDigits procedure % |
|||
for i := 5, 50, 9000 do begin |
|||
write(); |
|||
printBinary( i ); |
|||
end |
|||
end.</syntaxhighlight> |
|||
=={{header|ALGOL-M}}== |
=={{header|ALGOL-M}}== |
||
<syntaxhighlight lang=algolm>begin |
<syntaxhighlight lang="algolm">begin |
||
procedure writebin(n); |
procedure writebin(n); |
||
integer n; |
integer n; |
||
Line 679: | Line 690: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
Works in: [[Dyalog APL]] |
Works in: [[Dyalog APL]] |
||
A builtin function. Produces a boolean array. |
A builtin function. Produces a boolean array. |
||
<syntaxhighlight lang=apl>base2←2∘⊥⍣¯1</syntaxhighlight> |
<syntaxhighlight lang="apl">base2←2∘⊥⍣¯1</syntaxhighlight> |
||
Line 690: | Line 700: | ||
Produces a boolean array. |
Produces a boolean array. |
||
<syntaxhighlight lang=apl>base2 ← {((⌈2⍟⍵+1)⍴2)⊤⍵}</syntaxhighlight> |
<syntaxhighlight lang="apl">base2 ← {((⌈2⍟⍵+1)⍴2)⊤⍵}</syntaxhighlight> |
||
NOTE: Both versions above will yield an empty boolean array for 0. |
NOTE: Both versions above will yield an empty boolean array for 0. |
||
Line 704: | Line 714: | ||
1 0 0 0 1 1 0 0 1 0 1 0 0 0 |
1 0 0 0 1 1 0 0 1 0 1 0 0 0 |
||
</pre> |
</pre> |
||
=={{header|ALGOL W}}== |
|||
<syntaxhighlight lang=algolw>begin |
|||
% prints an integer in binary - the number must be greater than zero % |
|||
procedure printBinaryDigits( integer value n ) ; |
|||
begin |
|||
if n not = 0 then begin |
|||
printBinaryDigits( n div 2 ); |
|||
writeon( if n rem 2 = 1 then "1" else "0" ) |
|||
end |
|||
end binaryDigits ; |
|||
% prints an integer in binary - the number must not be negative % |
|||
procedure printBinary( integer value n ) ; |
|||
begin |
|||
if n = 0 then writeon( "0" ) |
|||
else printBinaryDigits( n ) |
|||
end printBinary ; |
|||
% test the printBinaryDigits procedure % |
|||
for i := 5, 50, 9000 do begin |
|||
write(); |
|||
printBinary( i ); |
|||
end |
|||
end.</syntaxhighlight> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Functional=== |
===Functional=== |
||
Line 737: | Line 720: | ||
(The generic showIntAtBase here, which allows us to specify the digit set used (e.g. upper or lower case in hex, or different regional or other digit sets generally), is a rough translation of Haskell's Numeric.showintAtBase) |
(The generic showIntAtBase here, which allows us to specify the digit set used (e.g. upper or lower case in hex, or different regional or other digit sets generally), is a rough translation of Haskell's Numeric.showintAtBase) |
||
<syntaxhighlight lang=applescript>---------------------- BINARY STRING ----------------------- |
<syntaxhighlight lang="applescript">---------------------- BINARY STRING ----------------------- |
||
-- showBin :: Int -> String |
-- showBin :: Int -> String |
||
Line 839: | Line 822: | ||
Or using: |
Or using: |
||
<syntaxhighlight lang=applescript>-- showBin :: Int -> String |
<syntaxhighlight lang="applescript">-- showBin :: Int -> String |
||
on showBin(n) |
on showBin(n) |
||
script binaryChar |
script binaryChar |
||
Line 857: | Line 840: | ||
At its very simplest, an AppleScript solution would look something like this: |
At its very simplest, an AppleScript solution would look something like this: |
||
<syntaxhighlight lang=applescript>on intToBinary(n) |
<syntaxhighlight lang="applescript">on intToBinary(n) |
||
set binary to (n mod 2 div 1) as text |
set binary to (n mod 2 div 1) as text |
||
set n to n div 2 |
set n to n div 2 |
||
Line 875: | Line 858: | ||
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: |
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: |
||
<syntaxhighlight lang=applescript>on intToBinary(n) |
<syntaxhighlight lang="applescript">on intToBinary(n) |
||
set binary to "" |
set binary to "" |
||
repeat |
repeat |
||
Line 898: | Line 881: | ||
intToBinary(50) & linefeed & ¬ |
intToBinary(50) & linefeed & ¬ |
||
intToBinary(9000) & linefeed</syntaxhighlight> |
intToBinary(9000) & linefeed</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="arm assembly"> |
||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 1,066: | Line 1,048: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
<syntaxhighlight lang=rebol>print as.binary 5 |
<syntaxhighlight lang="rebol">print as.binary 5 |
||
print as.binary 50 |
print as.binary 50 |
||
print as.binary 9000</syntaxhighlight> |
print as.binary 9000</syntaxhighlight> |
||
Line 1,077: | Line 1,058: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="autohotkey">MsgBox % NumberToBinary(5) ;101 |
||
MsgBox % NumberToBinary(50) ;110010 |
MsgBox % NumberToBinary(50) ;110010 |
||
MsgBox % NumberToBinary(9000) ;10001100101000 |
MsgBox % NumberToBinary(9000) ;10001100101000 |
||
Line 1,089: | Line 1,069: | ||
Return, Result |
Return, Result |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
<syntaxhighlight lang=autoit> |
<syntaxhighlight lang="autoit"> |
||
ConsoleWrite(IntToBin(50) & @CRLF) |
ConsoleWrite(IntToBin(50) & @CRLF) |
||
Line 1,108: | Line 1,087: | ||
EndFunc ;==>IntToBin |
EndFunc ;==>IntToBin |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang=awk>BEGIN { |
<syntaxhighlight lang="awk">BEGIN { |
||
print tobinary(5) |
print tobinary(5) |
||
print tobinary(50) |
print tobinary(50) |
||
Line 1,133: | Line 1,111: | ||
return outstr |
return outstr |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Axe}}== |
=={{header|Axe}}== |
||
This example builds a string backwards to ensure the digits are displayed in the correct order. It uses bitwise logic to extract one bit at a time. |
This example builds a string backwards to ensure the digits are displayed in the correct order. It uses bitwise logic to extract one bit at a time. |
||
<syntaxhighlight lang=axe>Lbl BIN |
<syntaxhighlight lang="axe">Lbl BIN |
||
.Axe supports 16-bit integers, so 16 digits are enough |
.Axe supports 16-bit integers, so 16 digits are enough |
||
L₁+16→P |
L₁+16→P |
||
Line 1,147: | Line 1,124: | ||
Disp P,i |
Disp P,i |
||
Return</syntaxhighlight> |
Return</syntaxhighlight> |
||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
<syntaxhighlight lang=freebasic>' Binary digits |
<syntaxhighlight lang="freebasic">' Binary digits |
||
OPTION MEMTYPE int |
OPTION MEMTYPE int |
||
INPUT n$ |
INPUT n$ |
||
Line 1,157: | Line 1,133: | ||
PRINT CHOP$(BIN$(VAL(n$)), "0", 1) |
PRINT CHOP$(BIN$(VAL(n$)), "0", 1) |
||
ENDIF</syntaxhighlight> |
ENDIF</syntaxhighlight> |
||
=={{header|Bash}}== |
|||
<syntaxhighlight lang="bash"> |
|||
function to_binary () { |
|||
if [ $1 -ge 0 ] |
|||
then |
|||
val=$1 |
|||
binary_digits=() |
|||
while [ $val -gt 0 ]; do |
|||
bit=$((val % 2)) |
|||
quotient=$((val / 2)) |
|||
binary_digits+=("${bit}") |
|||
val=$quotient |
|||
done |
|||
echo "${binary_digits[*]}" | rev |
|||
else |
|||
echo ERROR : "negative number" |
|||
exit 1 |
|||
fi |
|||
} |
|||
array=(5 50 9000) |
|||
for number in "${array[@]}"; do |
|||
echo $number " :> " $(to_binary $number) |
|||
done |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5 :> 1 0 1 |
|||
50 :> 1 1 0 0 1 0 |
|||
9000 :> 1 0 0 0 1 1 0 0 1 0 1 0 0 0 |
|||
</pre> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="applesoftbasic"> 0 N = 5: GOSUB 1:N = 50: GOSUB 1:N = 9000: GOSUB 1: END |
||
1 LET N2 = ABS ( INT (N)) |
1 LET N2 = ABS ( INT (N)) |
||
2 LET B$ = "" |
2 LET B$ = "" |
||
Line 1,178: | Line 1,185: | ||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
<syntaxhighlight lang=basic256> |
<syntaxhighlight lang="basic256"> |
||
# DecToBin.bas |
# DecToBin.bas |
||
# BASIC256 1.1.4.0 |
# BASIC256 1.1.4.0 |
||
Line 1,198: | Line 1,205: | ||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
<syntaxhighlight lang=bbcbasic> FOR num% = 0 TO 16 |
<syntaxhighlight lang="bbcbasic"> FOR num% = 0 TO 16 |
||
PRINT FN_tobase(num%, 2, 0) |
PRINT FN_tobase(num%, 2, 0) |
||
NEXT |
NEXT |
||
Line 1,216: | Line 1,223: | ||
The above is a generic "Convert to any base" program. |
The above is a generic "Convert to any base" program. |
||
Here is a faster "Convert to Binary" program: |
Here is a faster "Convert to Binary" program: |
||
<syntaxhighlight lang=bbcbasic>PRINT FNbinary(5) |
<syntaxhighlight lang="bbcbasic">PRINT FNbinary(5) |
||
PRINT FNbinary(50) |
PRINT FNbinary(50) |
||
PRINT FNbinary(9000) |
PRINT FNbinary(9000) |
||
Line 1,234: | Line 1,241: | ||
Note the <tt>FOR N1 =</tt> ... <tt>TO 0 STEP 0</tt> idiom; the zero step means that the variable is not modified by BASIC, so it's up to the code inside the loop to eventually set <tt>N1</tt> to 0 so that the loop terminates – like a C <tt>for</tt> loop with an empty third clause. After the initialization, it's essentially a "while N1 is not 0" loop, but Commodore BASIC originally didn't have <b>while</b> loops (<tt>DO WHILE</tt> ... <tt>LOOP</tt> was added in BASIC 3.5). The alternative would be a <tt>GOTO</tt>, but the <tt>FOR</tt> loop lends more structure. |
Note the <tt>FOR N1 =</tt> ... <tt>TO 0 STEP 0</tt> idiom; the zero step means that the variable is not modified by BASIC, so it's up to the code inside the loop to eventually set <tt>N1</tt> to 0 so that the loop terminates – like a C <tt>for</tt> loop with an empty third clause. After the initialization, it's essentially a "while N1 is not 0" loop, but Commodore BASIC originally didn't have <b>while</b> loops (<tt>DO WHILE</tt> ... <tt>LOOP</tt> was added in BASIC 3.5). The alternative would be a <tt>GOTO</tt>, but the <tt>FOR</tt> loop lends more structure. |
||
<syntaxhighlight lang=gwbasic>10 READ N |
<syntaxhighlight lang="gwbasic">10 READ N |
||
20 IF N < 0 THEN 70 |
20 IF N < 0 THEN 70 |
||
30 GOSUB 100 |
30 GOSUB 100 |
||
Line 1,257: | Line 1,264: | ||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="is-basic">10 PRINT BIN$(50) |
||
100 DEF BIN$(N) |
100 DEF BIN$(N) |
||
110 LET N=ABS(INT(N)):LET B$="" |
110 LET N=ABS(INT(N)):LET B$="" |
||
Line 1,267: | Line 1,274: | ||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="qbasic">FUNCTION BIN$ (N) |
||
N = ABS(INT(N)) |
N = ABS(INT(N)) |
||
B$ = "" |
B$ = "" |
||
Line 1,285: | Line 1,292: | ||
This turns into a horrible mess because of the lack of string concatenation in print statements, and the necessity of suppressing leading zeroes. |
This turns into a horrible mess because of the lack of string concatenation in print statements, and the necessity of suppressing leading zeroes. |
||
<syntaxhighlight lang=tinybasic}}>REM variables: |
<syntaxhighlight lang="tinybasic}}">REM variables: |
||
REM A-O: binary digits with A least significant and N most significant |
REM A-O: binary digits with A least significant and N most significant |
||
REM X: number whose binary expansion we want |
REM X: number whose binary expansion we want |
||
Line 1,371: | Line 1,378: | ||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
<syntaxhighlight lang=qbasic>FUNCTION BIN$ (N) |
<syntaxhighlight lang="qbasic">FUNCTION BIN$ (N) |
||
LET N = ABS(INT(N)) |
LET N = ABS(INT(N)) |
||
LET B$ = "" |
LET B$ = "" |
||
Line 1,390: | Line 1,397: | ||
PRINT " -> "; BIN$(9000) |
PRINT " -> "; BIN$(9000) |
||
END</syntaxhighlight> |
END</syntaxhighlight> |
||
=={{header|Bash}}== |
|||
<syntaxhighlight lang=BASH> |
|||
function to_binary () { |
|||
if [ $1 -ge 0 ] |
|||
then |
|||
val=$1 |
|||
binary_digits=() |
|||
while [ $val -gt 0 ]; do |
|||
bit=$((val % 2)) |
|||
quotient=$((val / 2)) |
|||
binary_digits+=("${bit}") |
|||
val=$quotient |
|||
done |
|||
echo "${binary_digits[*]}" | rev |
|||
else |
|||
echo ERROR : "negative number" |
|||
exit 1 |
|||
fi |
|||
} |
|||
array=(5 50 9000) |
|||
for number in "${array[@]}"; do |
|||
echo $number " :> " $(to_binary $number) |
|||
done |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5 :> 1 0 1 |
|||
50 :> 1 1 0 0 1 0 |
|||
9000 :> 1 0 0 0 1 1 0 0 1 0 1 0 0 0 |
|||
</pre> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
This num2bin.bat file handles non-negative input as per the requirements with no leading zeros in the output. Batch only supports signed integers. This script also handles negative values by printing the appropriate two's complement notation. |
This num2bin.bat file handles non-negative input as per the requirements with no leading zeros in the output. Batch only supports signed integers. This script also handles negative values by printing the appropriate two's complement notation. |
||
<syntaxhighlight lang=dos>@echo off |
<syntaxhighlight lang="dos">@echo off |
||
:num2bin IntVal [RtnVar] |
:num2bin IntVal [RtnVar] |
||
setlocal enableDelayedExpansion |
setlocal enableDelayedExpansion |
||
Line 1,440: | Line 1,413: | ||
) |
) |
||
exit /b</syntaxhighlight> |
exit /b</syntaxhighlight> |
||
=={{header|bc}}== |
=={{header|bc}}== |
||
{{trans|dc}} |
{{trans|dc}} |
||
<syntaxhighlight lang=bc>obase = 2 |
<syntaxhighlight lang="bc">obase = 2 |
||
5 |
5 |
||
50 |
50 |
||
9000 |
9000 |
||
quit</syntaxhighlight> |
quit</syntaxhighlight> |
||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
<syntaxhighlight lang=bcpl>get "libhdr" |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let writebin(x) be |
let writebin(x) be |
||
Line 1,470: | Line 1,441: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Beads}}== |
=={{header|Beads}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="beads">beads 1 program 'Binary Digits' |
||
calc main_init |
calc main_init |
||
loop across:[5, 50, 9000] val:v |
loop across:[5, 50, 9000] val:v |
||
Line 1,481: | Line 1,451: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
Reads the number to convert from standard input. |
Reads the number to convert from standard input. |
||
<syntaxhighlight lang=befunge>&>0\55+\:2%68>*#<+#8\#62#%/#2:_$>:#,_$@</syntaxhighlight> |
<syntaxhighlight lang="befunge">&>0\55+\:2%68>*#<+#8\#62#%/#2:_$>:#,_$@</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9000 |
<pre>9000 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
A BQNcrate idiom which returns the digits as a boolean array. |
A BQNcrate idiom which returns the digits as a boolean array. |
||
<syntaxhighlight lang=bqn>Bin ← 2{⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)} |
<syntaxhighlight lang="bqn">Bin ← 2{⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)} |
||
Bin¨5‿50‿9000</syntaxhighlight><syntaxhighlight lang=text>⟨ ⟨ 1 0 1 ⟩ ⟨ 1 1 0 0 1 0 ⟩ ⟨ 1 0 0 0 1 1 0 0 1 0 1 0 0 0 ⟩ ⟩</syntaxhighlight> |
|||
Bin¨5‿50‿9000</syntaxhighlight><syntaxhighlight lang="text">⟨ ⟨ 1 0 1 ⟩ ⟨ 1 1 0 0 1 0 ⟩ ⟨ 1 0 0 0 1 1 0 0 1 0 1 0 0 0 ⟩ ⟩</syntaxhighlight> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
<syntaxhighlight lang=bracmat> ( dec2bin |
<syntaxhighlight lang="bracmat"> ( dec2bin |
||
= bit bits |
= bit bits |
||
. :?bits |
. :?bits |
||
Line 1,529: | Line 1,496: | ||
423785674235000123456789: |
423785674235000123456789: |
||
1011001101111010111011110101001101111000000000000110001100000100111110100010101</pre> |
1011001101111010111011110101001101111000000000000110001100000100111110100010101</pre> |
||
=={{header|Brainf***}}== |
=={{header|Brainf***}}== |
||
This is almost an exact duplicate of [[Count in octal#Brainf***]]. It outputs binary numbers until it is forced to terminate or the counter overflows to 0. |
This is almost an exact duplicate of [[Count in octal#Brainf***]]. It outputs binary numbers until it is forced to terminate or the counter overflows to 0. |
||
<syntaxhighlight lang=bf>+[ Start with n=1 to kick off the loop |
<syntaxhighlight lang="bf">+[ Start with n=1 to kick off the loop |
||
[>>++<< Set up {n 0 2} for divmod magic |
[>>++<< Set up {n 0 2} for divmod magic |
||
[->+>- Then |
[->+>- Then |
||
Line 1,551: | Line 1,517: | ||
++++++++++. Print a newline |
++++++++++. Print a newline |
||
[-]<+] Zero it then increment n and go again</syntaxhighlight> |
[-]<+] Zero it then increment n and go again</syntaxhighlight> |
||
=={{header|Burlesque}}== |
=={{header|Burlesque}}== |
||
<syntaxhighlight lang=burlesque> |
<syntaxhighlight lang="burlesque"> |
||
blsq ) {5 50 9000}{2B!}m[uN |
blsq ) {5 50 9000}{2B!}m[uN |
||
101 |
101 |
||
Line 1,559: | Line 1,524: | ||
10001100101000 |
10001100101000 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
===With bit level operations=== |
===With bit level operations=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="c">#define _CRT_SECURE_NO_WARNINGS // turn off panic warnings |
||
#define _CRT_NONSTDC_NO_DEPRECATE // enable old-gold POSIX names in MSVS |
#define _CRT_NONSTDC_NO_DEPRECATE // enable old-gold POSIX names in MSVS |
||
Line 1,682: | Line 1,646: | ||
===With malloc and log10=== |
===With malloc and log10=== |
||
Converts int to a string. |
Converts int to a string. |
||
<syntaxhighlight lang=c>#include <math.h> |
<syntaxhighlight lang="c">#include <math.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,731: | Line 1,695: | ||
10010 |
10010 |
||
10011</pre> |
10011</pre> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
class Program |
class Program |
||
Line 1,745: | Line 1,708: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Another version using dotnet 5<syntaxhighlight lang=csharp dotnet 5.0>using System; |
Another version using dotnet 5<syntaxhighlight lang="csharp dotnet 5.0">using System; |
||
using System.Text; |
using System.Text; |
||
Line 1,765: | Line 1,728: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang=cpp>#include <bitset> |
<syntaxhighlight lang="cpp">#include <bitset> |
||
#include <iostream> |
#include <iostream> |
||
#include <limits> |
#include <limits> |
||
Line 1,798: | Line 1,760: | ||
</pre> |
</pre> |
||
Shorter version using bitset |
Shorter version using bitset |
||
<syntaxhighlight lang=cpp>#include <iostream> |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <bitset> |
#include <bitset> |
||
void printBits(int n) { // Use int like most programming languages. |
void printBits(int n) { // Use int like most programming languages. |
||
Line 1,813: | Line 1,775: | ||
} // for testing with n=0 printBits<32>(0);</syntaxhighlight> |
} // for testing with n=0 printBits<32>(0);</syntaxhighlight> |
||
Using >> operator. (1st example is 2.75x longer. Matter of taste.) |
Using >> operator. (1st example is 2.75x longer. Matter of taste.) |
||
<syntaxhighlight lang=cpp>#include <iostream> |
<syntaxhighlight lang="cpp">#include <iostream> |
||
int main(int argc, char* argv[]) { |
int main(int argc, char* argv[]) { |
||
unsigned int in[] = {5, 50, 9000}; // Use int like most programming languages |
unsigned int in[] = {5, 50, 9000}; // Use int like most programming languages |
||
Line 1,823: | Line 1,785: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
To be fair comparison with languages that doesn't declare a function like C++ main(). 3.14x shorter than 1st example. |
To be fair comparison with languages that doesn't declare a function like C++ main(). 3.14x shorter than 1st example. |
||
<syntaxhighlight lang=cpp>#include <iostream> |
<syntaxhighlight lang="cpp">#include <iostream> |
||
int main(int argc, char* argv[]) { // Usage: program.exe 5 50 9000 |
int main(int argc, char* argv[]) { // Usage: program.exe 5 50 9000 |
||
for (int i = 1; i < argc; i++) // argv[0] is program name |
for (int i = 1; i < argc; i++) // argv[0] is program name |
||
Line 1,832: | Line 1,794: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
Using bitwise operations with recursion. |
Using bitwise operations with recursion. |
||
<syntaxhighlight lang=cpp> |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,851: | Line 1,813: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
<syntaxhighlight lang=ceylon> shared void run() { |
<syntaxhighlight lang="ceylon"> shared void run() { |
||
void printBinary(Integer integer) => |
void printBinary(Integer integer) => |
||
Line 1,862: | Line 1,823: | ||
printBinary(9k); |
printBinary(9k); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<syntaxhighlight lang=clojure>(Integer/toBinaryString 5) |
<syntaxhighlight lang="clojure">(Integer/toBinaryString 5) |
||
(Integer/toBinaryString 50) |
(Integer/toBinaryString 50) |
||
(Integer/toBinaryString 9000)</syntaxhighlight> |
(Integer/toBinaryString 9000)</syntaxhighlight> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
<syntaxhighlight lang=clu>binary = proc (n: int) returns (string) |
<syntaxhighlight lang="clu">binary = proc (n: int) returns (string) |
||
bin: string := "" |
bin: string := "" |
||
while n > 0 do |
while n > 0 do |
||
Line 1,890: | Line 1,849: | ||
50 -> 110010 |
50 -> 110010 |
||
9000 -> 10001100101000</pre> |
9000 -> 10001100101000</pre> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. SAMPLE. |
PROGRAM-ID. SAMPLE. |
||
Line 1,920: | Line 1,878: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
Free-form, using a reference modifier to index into binary-number. |
Free-form, using a reference modifier to index into binary-number. |
||
<syntaxhighlight lang=cobol>IDENTIFICATION DIVISION. |
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION. |
||
PROGRAM-ID. binary-conversion. |
PROGRAM-ID. binary-conversion. |
||
Line 1,947: | Line 1,905: | ||
display binary-number. |
display binary-number. |
||
stop run.</syntaxhighlight> |
stop run.</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
<syntaxhighlight lang=coffeescript>binary = (n) -> |
<syntaxhighlight lang="coffeescript">binary = (n) -> |
||
new Number(n).toString(2) |
new Number(n).toString(2) |
||
console.log binary n for n in [5, 50, 9000]</syntaxhighlight> |
console.log binary n for n in [5, 50, 9000]</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Just print the number with "~b": |
Just print the number with "~b": |
||
<syntaxhighlight lang=lisp>(format t "~b" 5) |
<syntaxhighlight lang="lisp">(format t "~b" 5) |
||
; or |
; or |
||
(write 5 :base 2)</syntaxhighlight> |
(write 5 :base 2)</syntaxhighlight> |
||
=={{header|Component Pascal}}== |
=={{header|Component Pascal}}== |
||
BlackBox Component Builder |
BlackBox Component Builder |
||
<syntaxhighlight lang=oberon2> |
<syntaxhighlight lang="oberon2"> |
||
MODULE BinaryDigits; |
MODULE BinaryDigits; |
||
IMPORT StdLog,Strings; |
IMPORT StdLog,Strings; |
||
Line 1,987: | Line 1,942: | ||
50:> 110010 |
50:> 110010 |
||
9000:> 10001100101000</pre> |
9000:> 10001100101000</pre> |
||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
<syntaxhighlight lang=cowgol>include "cowgol.coh"; |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
sub print_binary(n: uint32) is |
sub print_binary(n: uint32) is |
||
Line 2,013: | Line 1,967: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
Using an array |
Using an array |
||
<syntaxhighlight lang=ruby>[5,50,9000].each do |n| |
<syntaxhighlight lang="ruby">[5,50,9000].each do |n| |
||
puts "%b" % n |
puts "%b" % n |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
Using a tuple |
Using a tuple |
||
<syntaxhighlight lang=ruby>{5,50,9000}.each { |n| puts n.to_s(2) }</syntaxhighlight> |
<syntaxhighlight lang="ruby">{5,50,9000}.each { |n| puts n.to_s(2) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>101 |
<pre>101 |
||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang=d>void main() { |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio; |
import std.stdio; |
||
Line 2,051: | Line 2,003: | ||
1110 |
1110 |
||
1111</pre> |
1111</pre> |
||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
<syntaxhighlight lang=dart>String binary(int n) { |
<syntaxhighlight lang="dart">String binary(int n) { |
||
if(n<0) |
if(n<0) |
||
throw new IllegalArgumentException("negative numbers require 2s complement"); |
throw new IllegalArgumentException("negative numbers require 2s complement"); |
||
Line 2,078: | Line 2,029: | ||
print(binary(0x123456789abcdef)); |
print(binary(0x123456789abcdef)); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|dc}}== |
=={{header|dc}}== |
||
<syntaxhighlight lang=dc>2o 5p 50p 9000p</syntaxhighlight> |
<syntaxhighlight lang="dc">2o 5p 50p 9000p</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,086: | Line 2,036: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="delphi"> |
||
program BinaryDigit; |
program BinaryDigit; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 2,114: | Line 2,063: | ||
9000: 10001100101000 |
9000: 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
A default <code>ToString</code> method of type <code>Integer</code> is overriden and returns a binary representation of a number: |
A default <code>ToString</code> method of type <code>Integer</code> is overriden and returns a binary representation of a number: |
||
<syntaxhighlight lang=dyalect>func Integer.ToString() { |
<syntaxhighlight lang="dyalect">func Integer.ToString() { |
||
var s = "" |
var s = "" |
||
for x in 31^-1..0 { |
for x in 31^-1..0 { |
||
Line 2,136: | Line 2,084: | ||
<pre>5 == 101, 50 = 110010, 1000 = 10001100101000</pre> |
<pre>5 == 101, 50 = 110010, 1000 = 10001100101000</pre> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<syntaxhighlight lang=text>func to2 n . r$ . |
<syntaxhighlight lang="text">func to2 n . r$ . |
||
if n > 0 |
if n > 0 |
||
call to2 n div 2 r$ |
call to2 n div 2 r$ |
||
Line 2,168: | Line 2,115: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
<syntaxhighlight lang=scheme> |
<syntaxhighlight lang="scheme"> |
||
;; primitive : (number->string number [base]) - default base = 10 |
;; primitive : (number->string number [base]) - default base = 10 |
||
Line 2,181: | Line 2,127: | ||
10001100101000 |
10001100101000 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
<syntaxhighlight lang=elena>import system'routines; |
<syntaxhighlight lang="elena">import system'routines; |
||
import extensions; |
import extensions; |
||
Line 2,200: | Line 2,145: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
Use <code>Integer.to_string</code> with a base of 2: |
Use <code>Integer.to_string</code> with a base of 2: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="elixir"> |
||
IO.puts Integer.to_string(5,2) |
IO.puts Integer.to_string(5,2) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Or, using the pipe operator: |
Or, using the pipe operator: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="elixir"> |
||
5 |> Integer.to_string(2) |> IO.puts |
5 |> Integer.to_string(2) |> IO.puts |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="elixir"> |
||
[5,50,9000] |> Enum.each(fn n -> IO.puts Integer.to_string(n,2) end) |
[5,50,9000] |> Enum.each(fn n -> IO.puts Integer.to_string(n,2) end) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 2,220: | Line 2,164: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Epoxy}}== |
=={{header|Epoxy}}== |
||
<syntaxhighlight lang=epoxy>fn bin(a,b:true) |
<syntaxhighlight lang="epoxy">fn bin(a,b:true) |
||
var c:"" |
var c:"" |
||
while a>0 do |
while a>0 do |
||
Line 2,244: | Line 2,187: | ||
9000: 10001100101000 |
9000: 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang=erlang>lists:map( fun(N) -> io:fwrite("~.2B~n", [N]) end, [5, 50, 9000]). </syntaxhighlight> |
<syntaxhighlight lang="erlang">lists:map( fun(N) -> io:fwrite("~.2B~n", [N]) end, [5, 50, 9000]). </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>101 |
<pre>101 |
||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
<syntaxhighlight lang=euphoria>function toBinary(integer i) |
<syntaxhighlight lang="euphoria">function toBinary(integer i) |
||
sequence s |
sequence s |
||
s = {} |
s = {} |
||
Line 2,268: | Line 2,209: | ||
=== Functional/Recursive === |
=== Functional/Recursive === |
||
<syntaxhighlight lang=euphoria>include std/math.e |
<syntaxhighlight lang="euphoria">include std/math.e |
||
include std/convert.e |
include std/convert.e |
||
Line 2,284: | Line 2,225: | ||
printf(1, "%d\n", Bin(50)) |
printf(1, "%d\n", Bin(50)) |
||
printf(1, "%d\n", Bin(9000))</syntaxhighlight> |
printf(1, "%d\n", Bin(9000))</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
By translating C#'s approach, using imperative coding style (inflexible): |
By translating C#'s approach, using imperative coding style (inflexible): |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="fsharp">open System |
||
for i in [5; 50; 9000] do printfn "%s" <| Convert.ToString (i, 2)</syntaxhighlight> |
for i in [5; 50; 9000] do printfn "%s" <| Convert.ToString (i, 2)</syntaxhighlight> |
||
Alternatively, by creating a function <code>printBin</code> which prints in binary (more flexible): |
Alternatively, by creating a function <code>printBin</code> which prints in binary (more flexible): |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="fsharp">open System |
||
// define the function |
// define the function |
||
Line 2,304: | Line 2,244: | ||
Or more idiomatic so that you can use it with any printf-style function and the <code>%a</code> format specifier (most flexible): |
Or more idiomatic so that you can use it with any printf-style function and the <code>%a</code> format specifier (most flexible): |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="fsharp">open System |
||
open System.IO |
open System.IO |
||
Line 2,320: | Line 2,260: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<syntaxhighlight lang=factor>USING: io kernel math math.parser ; |
<syntaxhighlight lang="factor">USING: io kernel math math.parser ; |
||
5 >bin print |
5 >bin print |
||
50 >bin print |
50 >bin print |
||
9000 >bin print</syntaxhighlight> |
9000 >bin print</syntaxhighlight> |
||
=={{header|FALSE}}== |
=={{header|FALSE}}== |
||
<syntaxhighlight lang=false>[0\10\[$1&'0+\2/$][]#%[$][,]#%]b: |
<syntaxhighlight lang="false">[0\10\[$1&'0+\2/$][]#%[$][,]#%]b: |
||
5 b;! |
5 b;! |
||
Line 2,338: | Line 2,276: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
<syntaxhighlight lang=fbsl>#AppType Console |
<syntaxhighlight lang="fbsl">#AppType Console |
||
function Bin(byval n as integer, byval s as string = "") as string |
function Bin(byval n as integer, byval s as string = "") as string |
||
if n > 0 then return Bin(n \ 2, (n mod 2) & s) |
if n > 0 then return Bin(n \ 2, (n mod 2) & s) |
||
Line 2,353: | Line 2,290: | ||
pause |
pause |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FOCAL}}== |
=={{header|FOCAL}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="focal">01.10 S A=5;D 2 |
||
01.20 S A=50;D 2 |
01.20 S A=50;D 2 |
||
01.30 S A=9000;D 2 |
01.30 S A=9000;D 2 |
||
Line 2,374: | Line 2,310: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<syntaxhighlight lang=forth>\ Forth uses a system variable 'BASE' for number conversion |
<syntaxhighlight lang="forth">\ Forth uses a system variable 'BASE' for number conversion |
||
\ HEX is a standard word to change the value of base to 16 |
\ HEX is a standard word to change the value of base to 16 |
||
Line 2,404: | Line 2,339: | ||
110000 |
110000 |
||
</pre> |
</pre> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Please find compilation instructions and the example run at the start of the FORTRAN90 source that follows. Thank you. |
Please find compilation instructions and the example run at the start of the FORTRAN90 source that follows. Thank you. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="fortran"> |
||
!-*- mode: compilation; default-directory: "/tmp/" -*- |
!-*- mode: compilation; default-directory: "/tmp/" -*- |
||
!Compilation started at Sun May 19 23:14:14 |
!Compilation started at Sun May 19 23:14:14 |
||
Line 2,466: | Line 2,400: | ||
end program bits |
end program bits |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Free Pascal}}== |
=={{header|Free Pascal}}== |
||
As part of the RTL (run-time library) that is shipped with every FPC (Free Pascal compiler) distribution, the <tt>system</tt> unit contains the function <tt>binStr</tt>. |
As part of the RTL (run-time library) that is shipped with every FPC (Free Pascal compiler) distribution, the <tt>system</tt> unit contains the function <tt>binStr</tt>. |
||
The <tt>system</tt> unit is automatically included by ''every'' program and is guaranteed to work on every supported platform. |
The <tt>system</tt> unit is automatically included by ''every'' program and is guaranteed to work on every supported platform. |
||
<syntaxhighlight lang=pascal>program binaryDigits(input, output, stdErr); |
<syntaxhighlight lang="pascal">program binaryDigits(input, output, stdErr); |
||
{$mode ISO} |
{$mode ISO} |
||
Line 2,494: | Line 2,427: | ||
end.</syntaxhighlight> |
end.</syntaxhighlight> |
||
Note, that the ISO compliant <tt>mod</tt> operation has to be used, which is ensured by the <tt>{$mode}</tt> directive in the second line. |
Note, that the ISO compliant <tt>mod</tt> operation has to be used, which is ensured by the <tt>{$mode}</tt> directive in the second line. |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang=freebasic> |
<syntaxhighlight lang="freebasic"> |
||
' FreeBASIC v1.05.0 win64 |
' FreeBASIC v1.05.0 win64 |
||
Dim As String fmt = "#### -> &" |
Dim As String fmt = "#### -> &" |
||
Line 2,514: | Line 2,446: | ||
9000 -> 10001100101000 |
9000 -> 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
The following all provide equivalent output. Input can be arbitrarily-large integers. |
The following all provide equivalent output. Input can be arbitrarily-large integers. |
||
<syntaxhighlight lang=frink> |
<syntaxhighlight lang="frink"> |
||
9000 -> binary |
9000 -> binary |
||
9000 -> base2 |
9000 -> base2 |
||
Line 2,523: | Line 2,454: | ||
base[9000, 2] |
base[9000, 2] |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
<syntaxhighlight lang=funl>for n <- [5, 50, 9000, 9000000000] |
<syntaxhighlight lang="funl">for n <- [5, 50, 9000, 9000000000] |
||
println( n, bin(n) )</syntaxhighlight> |
println( n, bin(n) )</syntaxhighlight> |
||
Line 2,536: | Line 2,466: | ||
9000000000, 1000011000011100010001101000000000 |
9000000000, 1000011000011100010001101000000000 |
||
</pre> |
</pre> |
||
=={{header|Futhark}}== |
=={{header|Futhark}}== |
||
We produce the binary number as a 64-bit integer whose digits are all 0s and 1s - this is because Futhark does not have any way to print, nor strings for that matter. |
We produce the binary number as a 64-bit integer whose digits are all 0s and 1s - this is because Futhark does not have any way to print, nor strings for that matter. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="futhark"> |
||
fun main(x: i32): i64 = |
fun main(x: i32): i64 = |
||
loop (out = 0i64) = for i < 32 do |
loop (out = 0i64) = for i < 32 do |
||
Line 2,549: | Line 2,478: | ||
in out |
in out |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
The decimal to binary conversion can be handled with a simple function. |
The decimal to binary conversion can be handled with a simple function. |
||
<syntaxhighlight lang=futurebasic> |
<syntaxhighlight lang="futurebasic"> |
||
include "NSLog.incl" |
include "NSLog.incl" |
||
Line 2,577: | Line 2,504: | ||
9000 = 10001100101000 |
9000 = 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=03e84768e6ee2af9b7664efa04fa6da8 Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=03e84768e6ee2af9b7664efa04fa6da8 Click this link to run this code]''' |
||
<syntaxhighlight lang=gambas>Public Sub Main() |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim siBin As Short[] = [5, 50, 9000] |
Dim siBin As Short[] = [5, 50, 9000] |
||
Dim siCount As Short |
Dim siCount As Short |
||
Line 2,596: | Line 2,521: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,628: | Line 2,552: | ||
1111 |
1111 |
||
</pre> |
</pre> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solutions: |
Solutions: |
||
<syntaxhighlight lang=groovy>print ''' |
<syntaxhighlight lang="groovy">print ''' |
||
n binary |
n binary |
||
----- --------------- |
----- --------------- |
||
Line 2,644: | Line 2,567: | ||
50 110010 |
50 110010 |
||
9000 10001100101000</pre> |
9000 10001100101000</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang=haskell>import Data.List |
<syntaxhighlight lang="haskell">import Data.List |
||
import Numeric |
import Numeric |
||
import Text.Printf |
import Text.Printf |
||
Line 2,681: | Line 2,603: | ||
and in terms of first and swap, we could also write this as: |
and in terms of first and swap, we could also write this as: |
||
<syntaxhighlight lang=haskell>import Data.Bifunctor (first) |
<syntaxhighlight lang="haskell">import Data.Bifunctor (first) |
||
import Data.List (unfoldr) |
import Data.List (unfoldr) |
||
import Data.Tuple (swap) |
import Data.Tuple (swap) |
||
Line 2,708: | Line 2,630: | ||
50 -> 110010 |
50 -> 110010 |
||
9000 -> 10001100101000</pre> |
9000 -> 10001100101000</pre> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
There is no built-in way to output the bit string representation of an whole number in Icon and Unicon. There are generalized radix conversion routines in the Icon Programming Library that comes with every distribution. This procedure is a customized conversion routine that will populate and use a tunable cache as it goes. |
There is no built-in way to output the bit string representation of an whole number in Icon and Unicon. There are generalized radix conversion routines in the Icon Programming Library that comes with every distribution. This procedure is a customized conversion routine that will populate and use a tunable cache as it goes. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="icon">procedure main() |
||
every i := 5 | 50 | 255 | 1285 | 9000 do |
every i := 5 | 50 | 255 | 1285 | 9000 do |
||
write(i," = ",binary(i)) |
write(i," = ",binary(i)) |
||
Line 2,740: | Line 2,661: | ||
1285 = 10100000101 |
1285 = 10100000101 |
||
9000 = 10001100101000</pre> |
9000 = 10001100101000</pre> |
||
=={{header|Idris}}== |
=={{header|Idris}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="idris">module Main |
||
binaryDigit : Integer -> Char |
binaryDigit : Integer -> Char |
||
Line 2,768: | Line 2,688: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang=j> tobin=: -.&' '@":@#: |
<syntaxhighlight lang="j"> tobin=: -.&' '@":@#: |
||
tobin 5 |
tobin 5 |
||
101 |
101 |
||
Line 2,780: | Line 2,699: | ||
I am using implicit output. |
I am using implicit output. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang=java>public class Main { |
<syntaxhighlight lang="java">public class Main { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
System.out.println(Integer.toBinaryString(5)); |
System.out.println(Integer.toBinaryString(5)); |
||
Line 2,793: | Line 2,711: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES5=== |
===ES5=== |
||
<syntaxhighlight lang=javascript>function toBinary(number) { |
<syntaxhighlight lang="javascript">function toBinary(number) { |
||
return new Number(number) |
return new Number(number) |
||
.toString(2); |
.toString(2); |
||
Line 2,809: | Line 2,726: | ||
The simplest showBinary (or showIntAtBase), using default digit characters, would use JavaScript's standard String.toString(base): |
The simplest showBinary (or showIntAtBase), using default digit characters, would use JavaScript's standard String.toString(base): |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 2,840: | Line 2,757: | ||
Or, if we need more flexibility with the set of digits used, we can write a version of showIntAtBase which takes a more specific Int -> Char function as as an argument. This one is a rough translation of Haskell's Numeric.showIntAtBase: |
Or, if we need more flexibility with the set of digits used, we can write a version of showIntAtBase which takes a more specific Int -> Char function as as an argument. This one is a rough translation of Haskell's Numeric.showIntAtBase: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 2,900: | Line 2,817: | ||
50 -> 一一〇〇一〇 |
50 -> 一一〇〇一〇 |
||
9000 -> 一〇〇〇一一〇〇一〇一〇〇〇</pre> |
9000 -> 一〇〇〇一一〇〇一〇一〇〇〇</pre> |
||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
<syntaxhighlight lang=joy>HIDE |
<syntaxhighlight lang="joy">HIDE |
||
_ == [null] [pop] [2 div swap] [48 + putch] linrec |
_ == [null] [pop] [2 div swap] [48 + putch] linrec |
||
IN |
IN |
||
Line 2,908: | Line 2,824: | ||
END</syntaxhighlight> |
END</syntaxhighlight> |
||
Using int2bin: |
Using int2bin: |
||
<syntaxhighlight lang=joy>0 setautoput |
<syntaxhighlight lang="joy">0 setautoput |
||
0 int2bin |
0 int2bin |
||
5 int2bin |
5 int2bin |
||
50 int2bin |
50 int2bin |
||
9000 int2bin.</syntaxhighlight> |
9000 int2bin.</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
<syntaxhighlight lang=jq>def binary_digits: |
<syntaxhighlight lang="jq">def binary_digits: |
||
[ recurse( ./2 | floor; . > 0) % 2 ] | reverse | join("") ; |
[ recurse( ./2 | floor; . > 0) % 2 ] | reverse | join("") ; |
||
Line 2,925: | Line 2,840: | ||
110010 |
110010 |
||
10001100101000 |
10001100101000 |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|1.0}} |
{{works with|Julia|1.0}} |
||
<syntaxhighlight lang=julia>using Printf |
<syntaxhighlight lang="julia">using Printf |
||
for n in (0, 5, 50, 9000) |
for n in (0, 5, 50, 9000) |
||
Line 2,952: | Line 2,866: | ||
50 → 00000000000000110010 |
50 → 00000000000000110010 |
||
9000 → 00000010001100101000</pre> |
9000 → 00000010001100101000</pre> |
||
=={{header|K}}== |
=={{header|K}}== |
||
<syntaxhighlight lang=k> tobin: ,/$2_vs |
<syntaxhighlight lang="k"> tobin: ,/$2_vs |
||
tobin' 5 50 9000 |
tobin' 5 50 9000 |
||
("101" |
("101" |
||
"110010" |
"110010" |
||
"10001100101000")</syntaxhighlight> |
"10001100101000")</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
<syntaxhighlight lang=scala>// version 1.0.5-2 |
<syntaxhighlight lang="scala">// version 1.0.5-2 |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 2,974: | Line 2,886: | ||
9000 -> 10001100101000 |
9000 -> 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
<syntaxhighlight lang=scheme> |
<syntaxhighlight lang="scheme"> |
||
{def dec2bin |
{def dec2bin |
||
{lambda {:dec} |
{lambda {:dec} |
||
Line 3,000: | Line 2,911: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Lang5}}== |
=={{header|Lang5}}== |
||
<syntaxhighlight lang=lang5>'%b '__number_format set |
<syntaxhighlight lang="lang5">'%b '__number_format set |
||
[5 50 9000] [3 1] reshape .</syntaxhighlight> |
[5 50 9000] [3 1] reshape .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,010: | Line 2,920: | ||
[ 10001100101000 ] |
[ 10001100101000 ] |
||
]</pre> |
]</pre> |
||
=={{header|LFE}}== |
=={{header|LFE}}== |
||
If one is simple printing the results and doesn't need to use them (e.g., assign them to any variables, etc.), this is very concise: |
If one is simple printing the results and doesn't need to use them (e.g., assign them to any variables, etc.), this is very concise: |
||
<syntaxhighlight lang=lisp> |
<syntaxhighlight lang="lisp"> |
||
(: io format '"~.2B~n~.2B~n~.2B~n" (list 5 50 9000)) |
(: io format '"~.2B~n~.2B~n~.2B~n" (list 5 50 9000)) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
If, however, you do need to get the results from a function, you can use <code>(: erlang integer_to_list ... )</code>. Here's a simple example that does the same thing as the previous code: |
If, however, you do need to get the results from a function, you can use <code>(: erlang integer_to_list ... )</code>. Here's a simple example that does the same thing as the previous code: |
||
<syntaxhighlight lang=lisp> |
<syntaxhighlight lang="lisp"> |
||
(: lists foreach |
(: lists foreach |
||
(lambda (x) |
(lambda (x) |
||
Line 3,033: | Line 2,942: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang=lb>for a = 0 to 16 |
<syntaxhighlight lang="lb">for a = 0 to 16 |
||
print a;"=";dec2bin$(a) |
print a;"=";dec2bin$(a) |
||
next |
next |
||
Line 3,051: | Line 2,959: | ||
end function |
end function |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Little Man Computer}}== |
=={{header|Little Man Computer}}== |
||
Runs in a home-made simulator, which is compatible with Peter Higginson's online simulator except that it has more room for output. Makes use of PH's non-standard OTC instruction to output ASCII characters. |
Runs in a home-made simulator, which is compatible with Peter Higginson's online simulator except that it has more room for output. Makes use of PH's non-standard OTC instruction to output ASCII characters. |
||
The maximum integer in LMC is 999, so 90000 in the task is here replaced by 900. |
The maximum integer in LMC is 999, so 90000 in the task is here replaced by 900. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="little man computer"> |
||
// Little Man Computer, for Rosetta Code. |
// Little Man Computer, for Rosetta Code. |
||
// Read numbers from user and display them in binary. |
// Read numbers from user and display them in binary. |
||
Line 3,123: | Line 3,030: | ||
900->1110000100 |
900->1110000100 |
||
</pre> |
</pre> |
||
=={{header|LLVM}}== |
=={{header|LLVM}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=llvm>; ModuleID = 'binary.c' |
<syntaxhighlight lang="llvm">; ModuleID = 'binary.c' |
||
; source_filename = "binary.c" |
; source_filename = "binary.c" |
||
; target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" |
; target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" |
||
Line 3,328: | Line 3,233: | ||
10010 |
10010 |
||
10011</pre> |
10011</pre> |
||
=={{header|Locomotive Basic}}== |
=={{header|Locomotive Basic}}== |
||
<syntaxhighlight lang=locobasic>10 PRINT BIN$(5) |
<syntaxhighlight lang="locobasic">10 PRINT BIN$(5) |
||
20 PRINT BIN$(50) |
20 PRINT BIN$(50) |
||
30 PRINT BIN$(9000)</syntaxhighlight> |
30 PRINT BIN$(9000)</syntaxhighlight> |
||
Line 3,337: | Line 3,241: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|LOLCODE}}== |
=={{header|LOLCODE}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lolcode">HAI 1.3 |
||
HOW IZ I DECIMULBINUR YR DECIMUL |
HOW IZ I DECIMULBINUR YR DECIMUL |
||
I HAS A BINUR ITZ "" |
I HAS A BINUR ITZ "" |
||
Line 3,360: | Line 3,263: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
===Lua - Iterative=== |
===Lua - Iterative=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lua">function dec2bin (n) |
||
local bin = "" |
local bin = "" |
||
while n > 0 do |
while n > 0 do |
||
Line 3,381: | Line 3,283: | ||
===Lua - Recursive=== |
===Lua - Recursive=== |
||
{{works with|Lua|5.3+}} |
{{works with|Lua|5.3+}} |
||
<syntaxhighlight lang=lua>function dec2bin(n, bin) |
<syntaxhighlight lang="lua">function dec2bin(n, bin) |
||
bin = (n&1) .. (bin or "") -- use n%2 instead of n&1 for Lua 5.1/5.2 |
bin = (n&1) .. (bin or "") -- use n%2 instead of n&1 for Lua 5.1/5.2 |
||
return n>1 and dec2bin(n//2, bin) or bin -- use math.floor(n/2) instead of n//2 for Lua 5.1/5.2 |
return n>1 and dec2bin(n//2, bin) or bin -- use math.floor(n/2) instead of n//2 for Lua 5.1/5.2 |
||
Line 3,393: | Line 3,295: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="m2000 interpreter"> |
||
Module Checkit { |
Module Checkit { |
||
Form 90, 40 |
Form 90, 40 |
||
Line 3,463: | Line 3,364: | ||
</pre > |
</pre > |
||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
MAD has basically no support for runtime generation of strings. |
MAD has basically no support for runtime generation of strings. |
||
Line 3,469: | Line 3,369: | ||
matches the binary representation of the input, e.g. <code>BINARY.(5)</code> is <code>101</code>. |
matches the binary representation of the input, e.g. <code>BINARY.(5)</code> is <code>101</code>. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
INTERNAL FUNCTION(NUM) |
INTERNAL FUNCTION(NUM) |
||
Line 3,494: | Line 3,394: | ||
50: 110010 |
50: 110010 |
||
9000: 10001100101000</pre> |
9000: 10001100101000</pre> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="maple"> |
||
> convert( 50, 'binary' ); |
> convert( 50, 'binary' ); |
||
110010 |
110010 |
||
Line 3,502: | Line 3,401: | ||
10001100101000 |
10001100101000 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">StringJoin @@ ToString /@ IntegerDigits[50, 2] </syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="matlab"> dec2bin(5) |
||
dec2bin(50) |
dec2bin(50) |
||
dec2bin(9000) </syntaxhighlight> |
dec2bin(9000) </syntaxhighlight> |
||
The output is a string containing ascii(48) (i.e. '0') and ascii(49) (i.e. '1'). |
The output is a string containing ascii(48) (i.e. '0') and ascii(49) (i.e. '1'). |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
<syntaxhighlight lang=maxima>digits([arg]) := block( |
<syntaxhighlight lang="maxima">digits([arg]) := block( |
||
[n: first(arg), b: if length(arg) > 1 then second(arg) else 10, v: [ ], q], |
[n: first(arg), b: if length(arg) > 1 then second(arg) else 10, v: [ ], q], |
||
do ( |
do ( |
||
Line 3,525: | Line 3,421: | ||
10001100101000 |
10001100101000 |
||
*/</syntaxhighlight> |
*/</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<syntaxhighlight lang=maxscript> |
<syntaxhighlight lang="maxscript"> |
||
-- MAXScript: Output decimal numbers from 0 to 16 as Binary : N.H. 2019 |
-- MAXScript: Output decimal numbers from 0 to 16 as Binary : N.H. 2019 |
||
for k = 0 to 16 do |
for k = 0 to 16 do |
||
Line 3,572: | Line 3,467: | ||
"10000" |
"10000" |
||
</pre> |
</pre> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
<syntaxhighlight lang=mercury>:- module binary_digits. |
<syntaxhighlight lang="mercury">:- module binary_digits. |
||
:- interface. |
:- interface. |
||
Line 3,591: | Line 3,485: | ||
io.write_string(int_to_base_string(N, 2), !IO), |
io.write_string(int_to_base_string(N, 2), !IO), |
||
io.nl(!IO).</syntaxhighlight> |
io.nl(!IO).</syntaxhighlight> |
||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
<syntaxhighlight lang=min>(2 over over mod 'div dip) :divmod2 |
<syntaxhighlight lang="min">(2 over over mod 'div dip) :divmod2 |
||
( |
( |
||
Line 3,610: | Line 3,503: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
=== Iterative === |
=== Iterative === |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="miniscript">binary = function(n) |
||
result = "" |
result = "" |
||
while n |
while n |
||
Line 3,629: | Line 3,521: | ||
=== Recursive === |
=== Recursive === |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="miniscript">binary = function(n,result="") |
||
if n == 0 then |
if n == 0 then |
||
if result == "" then return "0" else return result |
if result == "" then return "0" else return result |
||
Line 3,648: | Line 3,540: | ||
0 |
0 |
||
</pre> |
</pre> |
||
=={{header|mLite}}== |
=={{header|mLite}}== |
||
<syntaxhighlight lang=sml>fun binary |
<syntaxhighlight lang="sml">fun binary |
||
(0, b) = implode ` map (fn x = if int x then chr (x + 48) else x) b |
(0, b) = implode ` map (fn x = if int x then chr (x + 48) else x) b |
||
| (n, b) = binary (n div 2, n mod 2 :: b) |
| (n, b) = binary (n div 2, n mod 2 :: b) |
||
Line 3,665: | Line 3,556: | ||
> binary 9000; |
> binary 9000; |
||
"10001100101000"</pre> |
"10001100101000"</pre> |
||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
<syntaxhighlight lang=modula2>MODULE Binary; |
<syntaxhighlight lang="modula2">MODULE Binary; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM Terminal IMPORT Write,WriteLn,ReadChar; |
FROM Terminal IMPORT Write,WriteLn,ReadChar; |
||
Line 3,696: | Line 3,586: | ||
ReadChar |
ReadChar |
||
END Binary.</syntaxhighlight> |
END Binary.</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
<syntaxhighlight lang=modula3>MODULE Binary EXPORTS Main; |
<syntaxhighlight lang="modula3">MODULE Binary EXPORTS Main; |
||
IMPORT IO, Fmt; |
IMPORT IO, Fmt; |
||
Line 3,714: | Line 3,603: | ||
10010110 |
10010110 |
||
</pre> |
</pre> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 3,741: | Line 3,629: | ||
9000: 10001100101000 |
9000: 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|NewLisp}}== |
=={{header|NewLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="newlisp"> |
||
;;; Using the built-in "bits" function |
;;; Using the built-in "bits" function |
||
;;; For integers up to 9,223,372,036,854,775,807 |
;;; For integers up to 9,223,372,036,854,775,807 |
||
Line 3,766: | Line 3,653: | ||
1110100000110010010010000001110101110000001101101111110011101110001010110010111100010111111001011011001110001111110000101011010010 |
1110100000110010010010000001110101110000001101101111110011101110001010110010111100010111111001011011001110001111110000101011010010 |
||
</pre> |
</pre> |
||
=={{header|Nickle}}== |
=={{header|Nickle}}== |
||
Using the Nickle output radix operator: |
Using the Nickle output radix operator: |
||
Line 3,779: | Line 3,665: | ||
> 9000 # 2 |
> 9000 # 2 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<syntaxhighlight lang=nim>proc binDigits(x: BiggestInt, r: int): int = |
<syntaxhighlight lang="nim">proc binDigits(x: BiggestInt, r: int): int = |
||
## Calculates how many digits `x` has when each digit covers `r` bits. |
## Calculates how many digits `x` has when each digit covers `r` bits. |
||
result = 1 |
result = 1 |
||
Line 3,824: | Line 3,709: | ||
===Version using strformat=== |
===Version using strformat=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import strformat |
||
for n in 0..15: |
for n in 0..15: |
||
Line 3,846: | Line 3,731: | ||
1110 |
1110 |
||
1111</pre> |
1111</pre> |
||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
<syntaxhighlight lang=oberon2> |
<syntaxhighlight lang="oberon2"> |
||
MODULE BinaryDigits; |
MODULE BinaryDigits; |
||
IMPORT Out; |
IMPORT Out; |
||
Line 3,876: | Line 3,760: | ||
101010 |
101010 |
||
</pre> |
</pre> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
<syntaxhighlight lang=objeck>class Binary { |
<syntaxhighlight lang="objeck">class Binary { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
5->ToBinaryString()->PrintLine(); |
5->ToBinaryString()->PrintLine(); |
||
Line 3,891: | Line 3,774: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang=ocaml>let bin_of_int d = |
<syntaxhighlight lang="ocaml">let bin_of_int d = |
||
if d < 0 then invalid_arg "bin_of_int" else |
if d < 0 then invalid_arg "bin_of_int" else |
||
if d = 0 then "0" else |
if d = 0 then "0" else |
||
Line 3,905: | Line 3,787: | ||
let d = read_int () in |
let d = read_int () in |
||
Printf.printf "%8s\n" (bin_of_int d)</syntaxhighlight> |
Printf.printf "%8s\n" (bin_of_int d)</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 3,923: | Line 3,804: | ||
ok |
ok |
||
</pre> |
</pre> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
<syntaxhighlight lang=scheme> |
<syntaxhighlight lang="scheme"> |
||
(print (number->string 5 2)) |
(print (number->string 5 2)) |
||
(print (number->string 50 2)) |
(print (number->string 50 2)) |
||
Line 3,936: | Line 3,816: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|OxygenBasic}}== |
=={{header|OxygenBasic}}== |
||
The Assembly code uses block structures to minimise the use of labels. |
The Assembly code uses block structures to minimise the use of labels. |
||
<syntaxhighlight lang=oxygenbasic> |
<syntaxhighlight lang="oxygenbasic"> |
||
function BinaryBits(sys n) as string |
function BinaryBits(sys n) as string |
||
Line 3,984: | Line 3,863: | ||
print BinaryBits 0xaa 'result 10101010 |
print BinaryBits 0xaa 'result 10101010 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Panda}}== |
=={{header|Panda}}== |
||
<syntaxhighlight lang=panda>0..15.radix:2 nl</syntaxhighlight> |
<syntaxhighlight lang="panda">0..15.radix:2 nl</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 |
<pre>0 |
||
Line 4,004: | Line 3,882: | ||
1110 |
1110 |
||
1111</pre> |
1111</pre> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<syntaxhighlight lang=parigp>bin(n:int)=concat(apply(s->Str(s),binary(n)))</syntaxhighlight> |
<syntaxhighlight lang="parigp">bin(n:int)=concat(apply(s->Str(s),binary(n)))</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
FPC compiler Version 2.6 upwards.The obvious version. |
FPC compiler Version 2.6 upwards.The obvious version. |
||
<syntaxhighlight lang=pascal>program IntToBinTest; |
<syntaxhighlight lang="pascal">program IntToBinTest; |
||
{$MODE objFPC} |
{$MODE objFPC} |
||
uses |
uses |
||
Line 4,051: | Line 3,927: | ||
Beware of the endianess of the constant. |
Beware of the endianess of the constant. |
||
I check performance with random Data. |
I check performance with random Data. |
||
<syntaxhighlight lang=pascal> |
<syntaxhighlight lang="pascal"> |
||
program IntToPcharTest; |
program IntToPcharTest; |
||
uses |
uses |
||
Line 4,171: | Line 4,047: | ||
Time 0.175 secs, average stringlength 62.000 |
Time 0.175 secs, average stringlength 62.000 |
||
..the obvious version takes about 1.1 secs generating the string takes most of the time..</pre> |
..the obvious version takes about 1.1 secs generating the string takes most of the time..</pre> |
||
=={{header|Peloton}}== |
=={{header|Peloton}}== |
||
<syntaxhighlight lang=sgml><@ defbaslit>2</@> |
<syntaxhighlight lang="sgml"><@ defbaslit>2</@> |
||
<@ saybaslit>0</@> |
<@ saybaslit>0</@> |
||
Line 4,180: | Line 4,055: | ||
<@ saybaslit>9000</@> |
<@ saybaslit>9000</@> |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang=perl>for (5, 50, 9000) { |
<syntaxhighlight lang="perl">for (5, 50, 9000) { |
||
printf "%b\n", $_; |
printf "%b\n", $_; |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Line 4,190: | Line 4,064: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%b\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%b\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%b\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%b\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">)</span> |
||
Line 4,203: | Line 4,076: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="phixmonti">def printBinary |
||
"The decimal value " print dup print " should produce an output of " print |
"The decimal value " print dup print " should produce an output of " print |
||
20 int>bit |
20 int>bit |
||
Line 4,230: | Line 4,102: | ||
Other solution |
Other solution |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Binary_digits |
||
by Galileo, 05/2022 #/ |
by Galileo, 05/2022 #/ |
||
Line 4,255: | Line 4,127: | ||
=== Press any key to exit ===</pre> |
=== Press any key to exit ===</pre> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<syntaxhighlight lang=php><?php |
<syntaxhighlight lang="php"><?php |
||
echo decbin(5); |
echo decbin(5); |
||
echo decbin(50); |
echo decbin(50); |
||
Line 4,265: | Line 4,136: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat"> foreach(I in [5,50,900]) |
||
println(to_binary_string(I)) |
println(to_binary_string(I)) |
||
end.</syntaxhighlight> |
end.</syntaxhighlight> |
||
Line 4,275: | Line 4,145: | ||
110010 |
110010 |
||
1110000100</pre> |
1110000100</pre> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">: (bin 5) |
||
-> "101" |
-> "101" |
||
Line 4,286: | Line 4,154: | ||
: (bin 9000) |
: (bin 9000) |
||
-> "10001100101000"</syntaxhighlight> |
-> "10001100101000"</syntaxhighlight> |
||
=={{header|Piet}}== |
=={{header|Piet}}== |
||
Line 4,444: | Line 4,311: | ||
Explanation of program flow and image download link on my user page: [http://rosettacode.org/wiki/User:Albedo#Binary_Digits] |
Explanation of program flow and image download link on my user page: [http://rosettacode.org/wiki/User:Albedo#Binary_Digits] |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
Displays binary output trivially, but with leading zeros: |
Displays binary output trivially, but with leading zeros: |
||
<syntaxhighlight lang=pli>put edit (25) (B);</syntaxhighlight> |
<syntaxhighlight lang="pli">put edit (25) (B);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Output: 0011001 |
<pre>Output: 0011001 |
||
</pre> |
</pre> |
||
With leading zero suppression: |
With leading zero suppression: |
||
<syntaxhighlight lang=pli> declare text character (50) initial (' '); |
<syntaxhighlight lang="pli"> declare text character (50) initial (' '); |
||
put string(text) edit (25) (b); |
put string(text) edit (25) (b); |
||
Line 4,464: | Line 4,330: | ||
1111111111111111111111111111111 |
1111111111111111111111111111111 |
||
</pre> |
</pre> |
||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
<syntaxhighlight lang=plm>100H: |
<syntaxhighlight lang="plm">100H: |
||
/* CP/M BDOS CALL */ |
/* CP/M BDOS CALL */ |
||
Line 4,506: | Line 4,371: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
Pretty simple task in PowerBASIC since it has a built-in BIN$-Function. Omitting the second parameter ("Digits") means no leading zeros in the result. |
Pretty simple task in PowerBASIC since it has a built-in BIN$-Function. Omitting the second parameter ("Digits") means no leading zeros in the result. |
||
<syntaxhighlight lang=powerbasic> |
<syntaxhighlight lang="powerbasic"> |
||
#COMPILE EXE |
#COMPILE EXE |
||
#DIM ALL |
#DIM ALL |
||
Line 4,527: | Line 4,391: | ||
9000: 10001100101000 (00000000000000000010001100101000) |
9000: 10001100101000 (00000000000000000010001100101000) |
||
</pre> |
</pre> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{libheader|Microsoft .NET Framework}} |
{{libheader|Microsoft .NET Framework}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="powershell">@(5,50,900) | foreach-object { [Convert]::ToString($_,2) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>101 |
<pre>101 |
||
110010 |
110010 |
||
1110000100</pre> |
1110000100</pre> |
||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
<syntaxhighlight lang=processing>println(Integer.toBinaryString(5)); // 101 |
<syntaxhighlight lang="processing">println(Integer.toBinaryString(5)); // 101 |
||
println(Integer.toBinaryString(50)); // 110010 |
println(Integer.toBinaryString(50)); // 110010 |
||
println(Integer.toBinaryString(9000)); // 10001100101000</syntaxhighlight> |
println(Integer.toBinaryString(9000)); // 10001100101000</syntaxhighlight> |
||
Processing also has a binary() function, but this returns zero-padded results |
Processing also has a binary() function, but this returns zero-padded results |
||
<syntaxhighlight lang=processing>println(binary(5)); // 00000000000101 |
<syntaxhighlight lang="processing">println(binary(5)); // 00000000000101 |
||
println(binary(50)); // 00000000110010 |
println(binary(50)); // 00000000110010 |
||
println(binary(9000)); // 10001100101000</syntaxhighlight> |
println(binary(9000)); // 10001100101000</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
{{works with|GNU Prolog}} |
{{works with|GNU Prolog}} |
||
<syntaxhighlight lang=prolog> |
<syntaxhighlight lang="prolog"> |
||
binary(X) :- format('~2r~n', [X]). |
binary(X) :- format('~2r~n', [X]). |
||
main :- maplist(binary, [5,50,9000]), halt. |
main :- maplist(binary, [5,50,9000]), halt. |
||
Line 4,556: | Line 4,417: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="purebasic">If OpenConsole() |
||
PrintN(Bin(5)) ;101 |
PrintN(Bin(5)) ;101 |
||
PrintN(Bin(50)) ;110010 |
PrintN(Bin(50)) ;110010 |
||
Line 4,570: | Line 4,430: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
===String.format() method=== |
===String.format() method=== |
||
{{works with|Python|3.X and 2.6+}} |
{{works with|Python|3.X and 2.6+}} |
||
<syntaxhighlight lang=python>>>> for i in range(16): print('{0:b}'.format(i)) |
<syntaxhighlight lang="python">>>> for i in range(16): print('{0:b}'.format(i)) |
||
0 |
0 |
||
Line 4,595: | Line 4,454: | ||
===Built-in bin() function=== |
===Built-in bin() function=== |
||
{{works with|Python|3.X and 2.6+}} |
{{works with|Python|3.X and 2.6+}} |
||
<syntaxhighlight lang=python>>>> for i in range(16): print(bin(i)[2:]) |
<syntaxhighlight lang="python">>>> for i in range(16): print(bin(i)[2:]) |
||
0 |
0 |
||
Line 4,614: | Line 4,473: | ||
1111</syntaxhighlight> |
1111</syntaxhighlight> |
||
Pre-Python 2.6: |
Pre-Python 2.6: |
||
<syntaxhighlight lang=python>>>> oct2bin = {'0': '000', '1': '001', '2': '010', '3': '011', '4': '100', '5': '101', '6': '110', '7': '111'} |
<syntaxhighlight lang="python">>>> oct2bin = {'0': '000', '1': '001', '2': '010', '3': '011', '4': '100', '5': '101', '6': '110', '7': '111'} |
||
>>> bin = lambda n: ''.join(oct2bin[octdigit] for octdigit in '%o' % n).lstrip('0') or '0' |
>>> bin = lambda n: ''.join(oct2bin[octdigit] for octdigit in '%o' % n).lstrip('0') or '0' |
||
>>> for i in range(16): print(bin(i)) |
>>> for i in range(16): print(bin(i)) |
||
Line 4,638: | Line 4,497: | ||
Defined in terms of a more general '''showIntAtBase''' function: |
Defined in terms of a more general '''showIntAtBase''' function: |
||
<syntaxhighlight lang=python>'''Binary strings for integers''' |
<syntaxhighlight lang="python">'''Binary strings for integers''' |
||
Line 4,741: | Line 4,600: | ||
Or, using a more specialised function to decompose an integer to a list of boolean values: |
Or, using a more specialised function to decompose an integer to a list of boolean values: |
||
<syntaxhighlight lang=python>'''Decomposition of an integer to a string of booleans.''' |
<syntaxhighlight lang="python">'''Decomposition of an integer to a string of booleans.''' |
||
Line 4,872: | Line 4,731: | ||
50 -> 110010 |
50 -> 110010 |
||
9000 -> 10001100101000</pre> |
9000 -> 10001100101000</pre> |
||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="qb64"> |
||
Print DecToBin$(5) |
Print DecToBin$(5) |
||
Print DecToBin$(50) |
Print DecToBin$(50) |
||
Line 4,926: | Line 4,784: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Quackery provides built-in radix control, much like Forth. |
Quackery provides built-in radix control, much like Forth. |
||
<syntaxhighlight lang=quackery> |
<syntaxhighlight lang="quackery"> |
||
2 base put ( Numbers will be output in base 2 now. ) |
2 base put ( Numbers will be output in base 2 now. ) |
||
( Bases from 2 to 36 (inclusive) are supported. ) |
( Bases from 2 to 36 (inclusive) are supported. ) |
||
Line 4,943: | Line 4,800: | ||
A user-defined conversion might look something like this: |
A user-defined conversion might look something like this: |
||
<syntaxhighlight lang=quackery> |
<syntaxhighlight lang="quackery"> |
||
[ [] swap |
[ [] swap |
||
[ 2 /mod digit |
[ 2 /mod digit |
||
Line 4,960: | Line 4,817: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang=rsplus> |
<syntaxhighlight lang="rsplus"> |
||
dec2bin <- function(num) { |
dec2bin <- function(num) { |
||
ifelse(num == 0, |
ifelse(num == 0, |
||
Line 4,981: | Line 4,837: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket> |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
;; Option 1: binary formatter |
;; Option 1: binary formatter |
||
Line 4,990: | Line 4,845: | ||
(for ([i 16]) (displayln (number->string i 2))) |
(for ([i 16]) (displayln (number->string i 2))) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|2015.12}} |
{{works with|Rakudo|2015.12}} |
||
<syntaxhighlight lang=raku line>say .fmt("%b") for 5, 50, 9000;</syntaxhighlight> |
<syntaxhighlight lang="raku" line>say .fmt("%b") for 5, 50, 9000;</syntaxhighlight> |
||
<pre> |
<pre> |
||
101 |
101 |
||
Line 5,003: | Line 4,857: | ||
Alternatively: |
Alternatively: |
||
<syntaxhighlight lang=raku line>say .base(2) for 5, 50, 9000;</syntaxhighlight> |
<syntaxhighlight lang="raku" line>say .base(2) for 5, 50, 9000;</syntaxhighlight> |
||
<pre>101 |
<pre>101 |
||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
<syntaxhighlight lang=vb> |
<syntaxhighlight lang="vb"> |
||
'Convert Integer to binary string |
'Convert Integer to binary string |
||
Print "bin 5 = ", bin$(5) |
Print "bin 5 = ", bin$(5) |
||
Line 5,016: | Line 4,869: | ||
sleep 10 |
sleep 10 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="red">Red [] |
||
foreach number [5 50 9000] [ |
foreach number [5 50 9000] [ |
||
Line 5,031: | Line 4,883: | ||
9000 10001100101000 |
9000 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="retro">9000 50 5 3 [ binary putn cr decimal ] times</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This version handles the special case of zero simply. |
This version handles the special case of zero simply. |
||
Line 5,040: | Line 4,890: | ||
Note: some REXX interpreters have a '''D2B''' [Decimal to Binary] BIF ('''b'''uilt-'''i'''n '''f'''unction). |
Note: some REXX interpreters have a '''D2B''' [Decimal to Binary] BIF ('''b'''uilt-'''i'''n '''f'''unction). |
||
<br>Programming note: this REXX version depends on '''numeric digits''' being large enough to handle leading zeroes in this manner (by adding a zero (to the binary version) to force superfluous leading zero suppression). |
<br>Programming note: this REXX version depends on '''numeric digits''' being large enough to handle leading zeroes in this manner (by adding a zero (to the binary version) to force superfluous leading zero suppression). |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="rexx">/*REXX program to convert several decimal numbers to binary (or base 2). */ |
||
numeric digits 1000 /*ensure we can handle larger numbers. */ |
numeric digits 1000 /*ensure we can handle larger numbers. */ |
||
@.=; @.1= 0 |
@.=; @.1= 0 |
||
Line 5,062: | Line 4,912: | ||
This version handles the case of zero as a special case more elegantly. |
This version handles the case of zero as a special case more elegantly. |
||
<br>The following versions depend on the setting of '''numeric digits''' such that the number in decimal can be expressed as a whole number. |
<br>The following versions depend on the setting of '''numeric digits''' such that the number in decimal can be expressed as a whole number. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="rexx">/*REXX program to convert several decimal numbers to binary (or base 2). */ |
||
@.=; @.1= 0 |
@.=; @.1= 0 |
||
@.2= 5 |
@.2= 5 |
||
Line 5,077: | Line 4,927: | ||
===concise version=== |
===concise version=== |
||
This version handles the case of zero a bit more obtusely, but concisely. |
This version handles the case of zero a bit more obtusely, but concisely. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="rexx">/*REXX program to convert several decimal numbers to binary (or base 2). */ |
||
@.=; @.1= 0 |
@.=; @.1= 0 |
||
@.2= 5 |
@.2= 5 |
||
Line 5,091: | Line 4,941: | ||
===conforming version=== |
===conforming version=== |
||
This REXX version conforms to the strict output requirements of this task (just show the binary output without any blanks). |
This REXX version conforms to the strict output requirements of this task (just show the binary output without any blanks). |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="rexx">/*REXX program to convert several decimal numbers to binary (or base 2). */ |
||
numeric digits 200 /*ensure we can handle larger numbers. */ |
numeric digits 200 /*ensure we can handle larger numbers. */ |
||
@.=; @.1= 0 |
@.=; @.1= 0 |
||
Line 5,114: | Line 4,964: | ||
101010111111101001000101110110100000111011011011110111100110100100000100100001111101101110011101000101110110001101101000100100100110000111001010101011110010001111100011110100010101011011111111000110101110111100001011100111110000000010101100110101001010001001001011000000110000010010010100010010000001110100101000011111001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
101010111111101001000101110110100000111011011011110111100110100100000100100001111101101110011101000101110110001101101000100100100110000111001010101011110010001111100011110100010101011011111111000110101110111100001011100111110000000010101100110101001010001001001011000000110000010010010100010010000001110100101000011111001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
see "Number to convert : " |
see "Number to convert : " |
||
give a |
give a |
||
Line 5,130: | Line 4,979: | ||
next |
next |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=ruby>[5,50,9000].each do |n| |
<syntaxhighlight lang="ruby">[5,50,9000].each do |n| |
||
puts "%b" % n |
puts "%b" % n |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
or |
or |
||
<syntaxhighlight lang=ruby>for n in [5,50,9000] |
<syntaxhighlight lang="ruby">for n in [5,50,9000] |
||
puts n.to_s(2) |
puts n.to_s(2) |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
Line 5,143: | Line 4,991: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
<syntaxhighlight lang=runbasic>input "Number to convert:";a |
<syntaxhighlight lang="runbasic">input "Number to convert:";a |
||
while 2^(n+1) < a |
while 2^(n+1) < a |
||
n = n + 1 |
n = n + 1 |
||
Line 5,162: | Line 5,009: | ||
<pre>Number to convert:?9000 |
<pre>Number to convert:?9000 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang=rust>fn main() { |
<syntaxhighlight lang="rust">fn main() { |
||
for i in 0..8 { |
for i in 0..8 { |
||
println!("{:b}", i) |
println!("{:b}", i) |
||
Line 5,178: | Line 5,024: | ||
110 |
110 |
||
111</pre> |
111</pre> |
||
=={{header|S-lang}}== |
=={{header|S-lang}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="s-lang">define int_to_bin(d) |
||
{ |
{ |
||
variable m = 0x40000000, prn = 0, bs = ""; |
variable m = 0x40000000, prn = 0, bs = ""; |
||
Line 5,205: | Line 5,050: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Scala has an implicit conversion from <code>Int</code> to <code>RichInt</code> which has a method <code>toBinaryString</code>. |
Scala has an implicit conversion from <code>Int</code> to <code>RichInt</code> which has a method <code>toBinaryString</code>. |
||
<syntaxhighlight lang=scala>scala> (5 toBinaryString) |
<syntaxhighlight lang="scala">scala> (5 toBinaryString) |
||
res0: String = 101 |
res0: String = 101 |
||
Line 5,216: | Line 5,060: | ||
scala> (9000 toBinaryString) |
scala> (9000 toBinaryString) |
||
res2: String = 10001100101000</syntaxhighlight> |
res2: String = 10001100101000</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<syntaxhighlight lang=scheme>(display (number->string 5 2)) (newline) |
<syntaxhighlight lang="scheme">(display (number->string 5 2)) (newline) |
||
(display (number->string 50 2)) (newline) |
(display (number->string 50 2)) (newline) |
||
(display (number->string 9000 2)) (newline)</syntaxhighlight> |
(display (number->string 9000 2)) (newline)</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
This example uses the [http://seed7.sourceforge.net/libraries/integer.htm#%28in_integer%29radix%28in_integer%29 radix] operator to write a number in binary. |
This example uses the [http://seed7.sourceforge.net/libraries/integer.htm#%28in_integer%29radix%28in_integer%29 radix] operator to write a number in binary. |
||
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 5,255: | Line 5,097: | ||
10000 |
10000 |
||
</pre> |
</pre> |
||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
<syntaxhighlight lang=sequencel>main := toBinaryString([5, 50, 9000]); |
<syntaxhighlight lang="sequencel">main := toBinaryString([5, 50, 9000]); |
||
toBinaryString(number(0)) := |
toBinaryString(number(0)) := |
||
Line 5,271: | Line 5,112: | ||
["101","110010","10001100101000"] |
["101","110010","10001100101000"] |
||
</pre> |
</pre> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang=ruby>[5, 50, 9000].each { |n| |
<syntaxhighlight lang="ruby">[5, 50, 9000].each { |n| |
||
say n.as_bin; |
say n.as_bin; |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Line 5,280: | Line 5,120: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
<syntaxhighlight lang=simula>BEGIN |
<syntaxhighlight lang="simula">BEGIN |
||
PROCEDURE OUTINTBIN(N); INTEGER N; |
PROCEDURE OUTINTBIN(N); INTEGER N; |
||
Line 5,303: | Line 5,142: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|SkookumScript}}== |
=={{header|SkookumScript}}== |
||
<syntaxhighlight lang=javascript>println(5.binary) |
<syntaxhighlight lang="javascript">println(5.binary) |
||
println(50.binary) |
println(50.binary) |
||
println(9000.binary)</syntaxhighlight> |
println(9000.binary)</syntaxhighlight> |
||
Or looping over a list of numbers: |
Or looping over a list of numbers: |
||
<syntaxhighlight lang=javascript>{5 50 9000}.do[println(item.binary)]</syntaxhighlight> |
<syntaxhighlight lang="javascript">{5 50 9000}.do[println(item.binary)]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>101 |
<pre>101 |
||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
<syntaxhighlight lang=smalltalk>5 printOn: Stdout radix:2 |
<syntaxhighlight lang="smalltalk">5 printOn: Stdout radix:2 |
||
50 printOn: Stdout radix:2 |
50 printOn: Stdout radix:2 |
||
9000 printOn: Stdout radix:2</syntaxhighlight> |
9000 printOn: Stdout radix:2</syntaxhighlight> |
||
or: |
or: |
||
<syntaxhighlight lang=smalltalk>#(5 50 9000) do:[:each | each printOn: Stdout radix:2. Stdout cr]</syntaxhighlight> |
<syntaxhighlight lang="smalltalk">#(5 50 9000) do:[:each | each printOn: Stdout radix:2. Stdout cr]</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
<syntaxhighlight lang=snobol4> |
<syntaxhighlight lang="snobol4"> |
||
define('bin(n,r)') :(bin_end) |
define('bin(n,r)') :(bin_end) |
||
bin bin = le(n,0) r :s(return) |
bin bin = le(n,0) r :s(return) |
||
Line 5,340: | Line 5,176: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|SNUSP}}== |
=={{header|SNUSP}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="snusp"> |
||
/recurse\ |
/recurse\ |
||
$,binary!\@\>?!\@/<@\.# |
$,binary!\@\>?!\@/<@\.# |
||
Line 5,349: | Line 5,184: | ||
\?!#-?/+# mod2 |
\?!#-?/+# mod2 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<syntaxhighlight lang=sml>print (Int.fmt StringCvt.BIN 5 ^ "\n"); |
<syntaxhighlight lang="sml">print (Int.fmt StringCvt.BIN 5 ^ "\n"); |
||
print (Int.fmt StringCvt.BIN 50 ^ "\n"); |
print (Int.fmt StringCvt.BIN 50 ^ "\n"); |
||
print (Int.fmt StringCvt.BIN 9000 ^ "\n");</syntaxhighlight> |
print (Int.fmt StringCvt.BIN 9000 ^ "\n");</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="swift">for num in [5, 50, 9000] { |
||
println(String(num, radix: 2)) |
println(String(num, radix: 2)) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Line 5,364: | Line 5,197: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<syntaxhighlight lang=tcl>proc num2bin num { |
<syntaxhighlight lang="tcl">proc num2bin num { |
||
# Convert to _fixed width_ big-endian 32-bit binary |
# Convert to _fixed width_ big-endian 32-bit binary |
||
binary scan [binary format "I" $num] "B*" binval |
binary scan [binary format "I" $num] "B*" binval |
||
Line 5,373: | Line 5,205: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Demonstrating: |
Demonstrating: |
||
<syntaxhighlight lang=tcl>for {set x 0} {$x < 16} {incr x} { |
<syntaxhighlight lang="tcl">for {set x 0} {$x < 16} {incr x} { |
||
puts [num2bin $x] |
puts [num2bin $x] |
||
} |
} |
||
Line 5,405: | Line 5,237: | ||
<br> |
<br> |
||
Or you can use the builtin format: |
Or you can use the builtin format: |
||
<syntaxhighlight lang=tcl>foreach n {0 1 5 50 9000} { |
<syntaxhighlight lang="tcl">foreach n {0 1 5 50 9000} { |
||
puts [format "%4u: %b" $n $n] |
puts [format "%4u: %b" $n $n] |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Line 5,414: | Line 5,246: | ||
50: 110010 |
50: 110010 |
||
9000: 10001100101000</pre> |
9000: 10001100101000</pre> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Using Standard TI-83 BASIC |
Using Standard TI-83 BASIC |
||
<syntaxhighlight lang=ti83b>PROGRAM:BINARY |
<syntaxhighlight lang="ti83b">PROGRAM:BINARY |
||
:Disp "NUMBER TO" |
:Disp "NUMBER TO" |
||
:Disp "CONVERT:" |
:Disp "CONVERT:" |
||
Line 5,437: | Line 5,268: | ||
:Disp B</syntaxhighlight> |
:Disp B</syntaxhighlight> |
||
Alternate using a string to display larger numbers. |
Alternate using a string to display larger numbers. |
||
<syntaxhighlight lang=ti83b>PROGRAM:BINARY |
<syntaxhighlight lang="ti83b">PROGRAM:BINARY |
||
:Input X |
:Input X |
||
:" "→Str1 |
:" "→Str1 |
||
Line 5,447: | Line 5,278: | ||
:Str1</syntaxhighlight> |
:Str1</syntaxhighlight> |
||
Using the baseInput() "real(25," function from [http://www.detachedsolutions.com/omnicalc/ Omnicalc] |
Using the baseInput() "real(25," function from [http://www.detachedsolutions.com/omnicalc/ Omnicalc] |
||
<syntaxhighlight lang=ti83b>PROGRAM:BINARY |
<syntaxhighlight lang="ti83b">PROGRAM:BINARY |
||
:Disp "NUMBER TO" |
:Disp "NUMBER TO" |
||
:Disp "CONVERT" |
:Disp "CONVERT" |
||
Line 5,454: | Line 5,285: | ||
More compact version: |
More compact version: |
||
<syntaxhighlight lang=ti83b>:Input "DEC: ",D |
<syntaxhighlight lang="ti83b">:Input "DEC: ",D |
||
:" →Str1 |
:" →Str1 |
||
:If not(D:"0→Str1 |
:If not(D:"0→Str1 |
||
Line 5,467: | Line 5,298: | ||
:Disp Str1 |
:Disp Str1 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
This will convert any decimal number to any base between 2 and 16. |
This will convert any decimal number to any base between 2 and 16. |
||
<syntaxhighlight lang=text>Do |
<syntaxhighlight lang="text">Do |
||
Input "Enter base (1<X<17): "; b |
Input "Enter base (1<X<17): "; b |
||
While (b < 2) + (b > 16) |
While (b < 2) + (b > 16) |
||
Line 5,509: | Line 5,339: | ||
0 OK, 0:775</pre> |
0 OK, 0:775</pre> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
<syntaxhighlight lang=sh># Define a function to output binary digits |
<syntaxhighlight lang="sh"># Define a function to output binary digits |
||
tobinary() { |
tobinary() { |
||
# We use the bench calculator for our conversion |
# We use the bench calculator for our conversion |
||
Line 5,520: | Line 5,349: | ||
tobinary 5 |
tobinary 5 |
||
tobinary 50</syntaxhighlight> |
tobinary 50</syntaxhighlight> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
'''2 ways :''' |
'''2 ways :''' |
||
Line 5,541: | Line 5,369: | ||
Places is useful for padding the return value with leading 0s (zeros). |
Places is useful for padding the return value with leading 0s (zeros). |
||
<syntaxhighlight lang=vb> |
<syntaxhighlight lang="vb"> |
||
Option Explicit |
Option Explicit |
||
Line 5,588: | Line 5,416: | ||
The decimal value 9000 should produce an output of : 10001100101000 |
The decimal value 9000 should produce an output of : 10001100101000 |
||
The decimal value 9000 should produce an output of : Error : Number is too large ! (Number must be < 511)</pre> |
The decimal value 9000 should produce an output of : Error : Number is too large ! (Number must be < 511)</pre> |
||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
This implementation reads the numeric values from user input and writes the converted binary values in the edit buffer. |
This implementation reads the numeric values from user input and writes the converted binary values in the edit buffer. |
||
<syntaxhighlight lang=vedit>repeat (ALL) { |
<syntaxhighlight lang="vedit">repeat (ALL) { |
||
#10 = Get_Num("Give a numeric value, -1 to end: ", STATLINE) |
#10 = Get_Num("Give a numeric value, -1 to end: ", STATLINE) |
||
if (#10 < 0) { break } |
if (#10 < 0) { break } |
||
Line 5,616: | Line 5,443: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Vim Script}}== |
=={{header|Vim Script}}== |
||
<syntaxhighlight lang=vim>function Num2Bin(n) |
<syntaxhighlight lang="vim">function Num2Bin(n) |
||
let n = a:n |
let n = a:n |
||
let s = "" |
let s = "" |
||
Line 5,644: | Line 5,470: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
||
{{works with|Visual Basic|VB6 Standard}} |
{{works with|Visual Basic|VB6 Standard}} |
||
<syntaxhighlight lang=vb> |
<syntaxhighlight lang="vb"> |
||
Public Function Bin(ByVal l As Long) As String |
Public Function Bin(ByVal l As Long) As String |
||
Dim i As Long |
Dim i As Long |
||
Line 5,687: | Line 5,512: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
<syntaxhighlight lang=vbnet>Module Program |
<syntaxhighlight lang="vbnet">Module Program |
||
Sub Main |
Sub Main |
||
For Each number In {5, 50, 9000} |
For Each number In {5, 50, 9000} |
||
Line 5,700: | Line 5,524: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Visual FoxPro}}== |
=={{header|Visual FoxPro}}== |
||
<syntaxhighlight lang=vfp> |
<syntaxhighlight lang="vfp"> |
||
*!* Binary Digits |
*!* Binary Digits |
||
CLEAR |
CLEAR |
||
Line 5,735: | Line 5,558: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|Vlang}}== |
|||
<syntaxhighlight lang="vlang">fn main() { |
|||
for i in 0..16 { |
|||
println("${i:b}") |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0 |
|||
1 |
|||
10 |
|||
11 |
|||
100 |
|||
101 |
|||
110 |
|||
111 |
|||
1000 |
|||
1001 |
|||
1010 |
|||
1011 |
|||
1100 |
|||
1101 |
|||
1110 |
|||
1111 |
|||
</pre> |
|||
=={{header|VTL-2}}== |
|||
<syntaxhighlight lang="vtl2">10 N=5 |
|||
20 #=100 |
|||
30 N=50 |
|||
40 #=100 |
|||
50 N=9000 |
|||
100 ;=! |
|||
110 I=18 |
|||
120 I=I-1 |
|||
130 N=N/2 |
|||
140 :I)=% |
|||
150 #=0<N*120 |
|||
160 ?=:I) |
|||
170 I=I+1 |
|||
180 #=I<18*160 |
|||
190 ?="" |
|||
200 #=;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>101 |
|||
110010 |
|||
10001100101000</pre> |
|||
=={{header|Whitespace}}== |
=={{header|Whitespace}}== |
||
This program prints binary numbers until the internal representation of the current integer overflows to -1; it will never do so on some interpreters. It is almost an exact duplicate of [[Count in octal#Whitespace]]. |
This program prints binary numbers until the internal representation of the current integer overflows to -1; it will never do so on some interpreters. It is almost an exact duplicate of [[Count in octal#Whitespace]]. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="whitespace"> |
||
Line 5,783: | Line 5,651: | ||
It was generated from the following pseudo-Assembly. |
It was generated from the following pseudo-Assembly. |
||
<syntaxhighlight lang=asm>push 0 |
<syntaxhighlight lang="asm">push 0 |
||
; Increment indefinitely. |
; Increment indefinitely. |
||
0: |
0: |
||
Line 5,816: | Line 5,684: | ||
pop |
pop |
||
ret</syntaxhighlight> |
ret</syntaxhighlight> |
||
=={{header|Vlang}}== |
|||
<syntaxhighlight lang=vlang>fn main() { |
|||
for i in 0..16 { |
|||
println("${i:b}") |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0 |
|||
1 |
|||
10 |
|||
11 |
|||
100 |
|||
101 |
|||
110 |
|||
111 |
|||
1000 |
|||
1001 |
|||
1010 |
|||
1011 |
|||
1100 |
|||
1101 |
|||
1110 |
|||
1111 |
|||
</pre> |
|||
=={{header|VTL-2}}== |
|||
<syntaxhighlight lang=VTL2>10 N=5 |
|||
20 #=100 |
|||
30 N=50 |
|||
40 #=100 |
|||
50 N=9000 |
|||
100 ;=! |
|||
110 I=18 |
|||
120 I=I-1 |
|||
130 N=N/2 |
|||
140 :I)=% |
|||
150 #=0<N*120 |
|||
160 ?=:I) |
|||
170 I=I+1 |
|||
180 #=I<18*160 |
|||
190 ?="" |
|||
200 #=;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>101 |
|||
110010 |
|||
10001100101000</pre> |
|||
=={{header|Wortel}}== |
=={{header|Wortel}}== |
||
Using JavaScripts buildin toString method on the Number object, the following function takes a number and returns a string with the binary representation: |
Using JavaScripts buildin toString method on the Number object, the following function takes a number and returns a string with the binary representation: |
||
<syntaxhighlight lang=wortel>\.toString 2 |
<syntaxhighlight lang="wortel">\.toString 2 |
||
; the following function also casts the string to a number |
; the following function also casts the string to a number |
||
^(@+ \.toString 2)</syntaxhighlight> |
^(@+ \.toString 2)</syntaxhighlight> |
||
To output to the console: |
To output to the console: |
||
<syntaxhighlight lang=wortel>@each ^(console.log \.toString 2) [5 50 900]</syntaxhighlight> |
<syntaxhighlight lang="wortel">@each ^(console.log \.toString 2) [5 50 900]</syntaxhighlight> |
||
Outputs: <pre> |
Outputs: <pre> |
||
101 |
101 |
||
110010 |
110010 |
||
1110000100</pre> |
1110000100</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
System.print("Converting to binary:") |
System.print("Converting to binary:") |
||
Line 5,891: | Line 5,709: | ||
9000 -> 10001100101000 |
9000 -> 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|X86 Assembly}}== |
=={{header|X86 Assembly}}== |
||
Translation of XPL0. Assemble with tasm, tlink /t |
Translation of XPL0. Assemble with tasm, tlink /t |
||
<syntaxhighlight lang=asm> .model tiny |
<syntaxhighlight lang="asm"> .model tiny |
||
.code |
.code |
||
.486 |
.486 |
||
Line 5,930: | Line 5,747: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic code declarations |
||
proc BinOut(N); \Output N in binary |
proc BinOut(N); \Output N in binary |
||
Line 5,967: | Line 5,783: | ||
100000010100001 |
100000010100001 |
||
</pre> |
</pre> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
<syntaxhighlight lang=yabasic>dim a(3) |
<syntaxhighlight lang="yabasic">dim a(3) |
||
a(0) = 5 |
a(0) = 5 |
||
a(1) = 50 |
a(1) = 50 |
||
Line 5,978: | Line 5,793: | ||
next i |
next i |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
=={{header|Z80 Assembly}}== |
=={{header|Z80 Assembly}}== |
||
{{trans|8086 Assembly}} |
{{trans|8086 Assembly}} |
||
<syntaxhighlight lang=z80>org &8000 |
<syntaxhighlight lang="z80">org &8000 |
||
PrintChar equ &BB5A ;syscall - prints accumulator to Amstrad CPC's screen |
PrintChar equ &BB5A ;syscall - prints accumulator to Amstrad CPC's screen |
||
Line 6,058: | Line 5,872: | ||
This is another version. Output of the result over port 0A hex. |
This is another version. Output of the result over port 0A hex. |
||
<syntaxhighlight lang=z80> |
<syntaxhighlight lang="z80"> |
||
; HL contains the value to be converted |
; HL contains the value to be converted |
||
ld hl,5 |
ld hl,5 |
||
Line 6,104: | Line 5,918: | ||
djnz bitloop |
djnz bitloop |
||
ret</syntaxhighlight> |
ret</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
<syntaxhighlight lang=zkl>(9000).toString(2)</syntaxhighlight> |
<syntaxhighlight lang="zkl">(9000).toString(2)</syntaxhighlight> |
||
<syntaxhighlight lang=zkl>T(5,50,9000).apply("toString",2) //--> L("101","110010","10001100101000")</syntaxhighlight> |
<syntaxhighlight lang="zkl">T(5,50,9000).apply("toString",2) //--> L("101","110010","10001100101000")</syntaxhighlight> |
||
<syntaxhighlight lang=zkl>"%.2B".fmt(9000)</syntaxhighlight> |
<syntaxhighlight lang="zkl">"%.2B".fmt(9000)</syntaxhighlight> |
||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
<syntaxhighlight lang=zxbasic>10 LET n=5: GO SUB 1000: PRINT s$ |
<syntaxhighlight lang="zxbasic">10 LET n=5: GO SUB 1000: PRINT s$ |
||
20 LET n=50: GO SUB 1000: PRINT s$ |
20 LET n=50: GO SUB 1000: PRINT s$ |
||
30 LET n=9000: GO SUB 1000: PRINT s$ |
30 LET n=9000: GO SUB 1000: PRINT s$ |