Binary digits: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 511: | Line 511: | ||
end.</lang> |
end.</lang> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 818: | Line 817: | ||
Return, Result |
Return, Result |
||
}</lang> |
}</lang> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
<lang autoit> |
<lang autoit> |
||
Line 885: | Line 885: | ||
PRINT CHOP$(BIN$(VAL(n$)), "0", 1) |
PRINT CHOP$(BIN$(VAL(n$)), "0", 1) |
||
ENDIF</lang> |
ENDIF</lang> |
||
=={{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. |
|||
<lang dos>@echo off |
|||
:num2bin IntVal [RtnVar] |
|||
setlocal enableDelayedExpansion |
|||
set /a n=%~1 |
|||
set rtn= |
|||
for /l %%b in (0,1,31) do ( |
|||
set /a "d=n&1, n>>=1" |
|||
set rtn=!d!!rtn! |
|||
) |
|||
for /f "tokens=* delims=0" %%a in ("!rtn!") do set rtn=%%a |
|||
(endlocal & rem -- return values |
|||
if "%~2" neq "" (set %~2=%rtn%) else echo %rtn% |
|||
) |
|||
exit /b</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
Line 999: | Line 982: | ||
160 LET BIN$=B$ |
160 LET BIN$=B$ |
||
170 END DEF</lang> |
170 END DEF</lang> |
||
=={{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. |
|||
<lang dos>@echo off |
|||
:num2bin IntVal [RtnVar] |
|||
setlocal enableDelayedExpansion |
|||
set /a n=%~1 |
|||
set rtn= |
|||
for /l %%b in (0,1,31) do ( |
|||
set /a "d=n&1, n>>=1" |
|||
set rtn=!d!!rtn! |
|||
) |
|||
for /f "tokens=* delims=0" %%a in ("!rtn!") do set rtn=%%a |
|||
(endlocal & rem -- return values |
|||
if "%~2" neq "" (set %~2=%rtn%) else echo %rtn% |
|||
) |
|||
exit /b</lang> |
|||
=={{header|bc}}== |
=={{header|bc}}== |
||
Line 1,007: | Line 1,007: | ||
9000 |
9000 |
||
quit</lang> |
quit</lang> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
Reads the number to convert from standard input. |
Reads the number to convert from standard input. |
||
Line 1,129: | Line 1,130: | ||
10010 |
10010 |
||
10011</pre> |
10011</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
class Program |
|||
{ |
|||
static void Main() |
|||
{ |
|||
foreach (var number in new[] { 5, 50, 9000 }) |
|||
{ |
|||
Console.WriteLine(Convert.ToString(number, 2)); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
101 |
|||
110010 |
|||
10001100101000 |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 1,209: | Line 1,230: | ||
} |
} |
||
</lang> |
</lang> |
||
{{out}} |
|||
<pre> |
|||
101 |
|||
110010 |
|||
10001100101000 |
|||
</pre> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
class Program |
|||
{ |
|||
static void Main() |
|||
{ |
|||
foreach (var number in new[] { 5, 50, 9000 }) |
|||
{ |
|||
Console.WriteLine(Convert.ToString(number, 2)); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,449: | Line 1,450: | ||
9000: 10001100101000 |
9000: 10001100101000 |
||
</pre> |
</pre> |
||
=={{header|EchoLisp}}== |
|||
<lang scheme> |
|||
;; primitive : (number->string number [base]) - default base = 10 |
|||
(number->string 2 2) |
|||
→ 10 |
|||
(for-each (compose writeln (rcurry number->string 2)) '( 5 50 9000)) → |
|||
101 |
|||
110010 |
|||
10001100101000 |
|||
</lang> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
Line 1,516: | Line 1,504: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|EchoLisp}}== |
|||
<lang scheme> |
|||
;; primitive : (number->string number [base]) - default base = 10 |
|||
(number->string 2 2) |
|||
→ 10 |
|||
(for-each (compose writeln (rcurry number->string 2)) '( 5 50 9000)) → |
|||
101 |
|||
110010 |
|||
10001100101000 |
|||
</lang> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
Line 1,737: | Line 1,738: | ||
end program bits |
end program bits |
||
</lang> |
</lang> |
||
=={{header|FreeBASIC}}== |
|||
<lang freebasic> |
|||
' FreeBASIC v1.05.0 win64 |
|||
Dim As String fmt = "#### -> &" |
|||
Print Using fmt; 5; Bin(5) |
|||
Print Using fmt; 50; Bin(50) |
|||
Print Using fmt; 9000; Bin(9000) |
|||
Print |
|||
Print "Press any key to exit the program" |
|||
Sleep |
|||
End |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
5 -> 101 |
|||
50 -> 110010 |
|||
9000 -> 10001100101000 |
|||
</pre> |
|||
=={{header|Free Pascal}}== |
=={{header|Free Pascal}}== |
||
Line 1,785: | Line 1,766: | ||
end.</lang> |
end.</lang> |
||
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}}== |
|||
<lang freebasic> |
|||
' FreeBASIC v1.05.0 win64 |
|||
Dim As String fmt = "#### -> &" |
|||
Print Using fmt; 5; Bin(5) |
|||
Print Using fmt; 50; Bin(50) |
|||
Print Using fmt; 9000; Bin(9000) |
|||
Print |
|||
Print "Press any key to exit the program" |
|||
Sleep |
|||
End |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
5 -> 101 |
|||
50 -> 110010 |
|||
9000 -> 10001100101000 |
|||
</pre> |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Line 1,951: | Line 1,952: | ||
1285 = 10100000101 |
1285 = 10100000101 |
||
9000 = 10001100101000</pre> |
9000 = 10001100101000</pre> |
||
=={{header|Idris}}== |
=={{header|Idris}}== |
||
Line 2,122: | Line 2,122: | ||
50 int2bin |
50 int2bin |
||
9000 int2bin.</lang> |
9000 int2bin.</lang> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
<lang jq>def binary_digits: |
<lang jq>def binary_digits: |
||
Line 2,579: | Line 2,580: | ||
dec2bin(9000) </lang> |
dec2bin(9000) </lang> |
||
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}}== |
|||
<lang maxima>digits([arg]) := block( |
|||
[n: first(arg), b: if length(arg) > 1 then second(arg) else 10, v: [ ], q], |
|||
do ( |
|||
[n, q]: divide(n, b), |
|||
v: cons(q, v), |
|||
if n=0 then return(v)))$ |
|||
binary(n) := simplode(digits(n, 2))$ |
|||
binary(9000); |
|||
/* |
|||
10001100101000 |
|||
*/</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 2,626: | Line 2,641: | ||
"10000" |
"10000" |
||
</pre> |
</pre> |
||
=={{header|Maxima}}== |
|||
<lang maxima>digits([arg]) := block( |
|||
[n: first(arg), b: if length(arg) > 1 then second(arg) else 10, v: [ ], q], |
|||
do ( |
|||
[n, q]: divide(n, b), |
|||
v: cons(q, v), |
|||
if n=0 then return(v)))$ |
|||
binary(n) := simplode(digits(n, 2))$ |
|||
binary(9000); |
|||
/* |
|||
10001100101000 |
|||
*/</lang> |
|||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
Line 2,990: | Line 2,991: | ||
print BinaryBits 0xaa 'result 10101010 |
print BinaryBits 0xaa 'result 10101010 |
||
</lang> |
</lang> |
||
=={{header|PARI/GP}}== |
|||
<lang parigp>bin(n:int)=concat(apply(s->Str(s),binary(n)))</lang> |
|||
=={{header|Panda}}== |
=={{header|Panda}}== |
||
Line 3,013: | Line 3,011: | ||
1110 |
1110 |
||
1111</pre> |
1111</pre> |
||
=={{header|PARI/GP}}== |
|||
<lang parigp>bin(n:int)=concat(apply(s->Str(s),binary(n)))</lang> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
Line 3,190: | Line 3,192: | ||
printf "%b\n", $_; |
printf "%b\n", $_; |
||
}</lang> |
}</lang> |
||
<pre> |
|||
101 |
|||
110010 |
|||
10001100101000 |
|||
</pre> |
|||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>say .fmt("%b") for 5, 50, 9000;</lang> |
|||
<pre> |
<pre> |
||
101 |
101 |
||
Line 3,413: | Line 3,406: | ||
1111111111111111111111111111111 |
1111111111111111111111111111111 |
||
</pre> |
</pre> |
||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
Line 3,435: | Line 3,427: | ||
9000: 10001100101000 (00000000000000000010001100101000) |
9000: 10001100101000 (00000000000000000010001100101000) |
||
</pre> |
</pre> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 3,781: | Line 3,772: | ||
50 -> 110010 |
50 -> 110010 |
||
9000 -> 10001100101000</pre> |
9000 -> 10001100101000</pre> |
||
=={{header|R}}== |
=={{header|R}}== |
||
<lang rsplus> |
<lang rsplus> |
||
Line 3,810: | Line 3,802: | ||
(for ([i 16]) (displayln (number->string i 2))) |
(for ([i 16]) (displayln (number->string i 2))) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>say .fmt("%b") for 5, 50, 9000;</lang> |
|||
<pre> |
|||
101 |
|||
110010 |
|||
10001100101000 |
|||
</pre> |
|||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
<lang vb> |
<lang vb> |
||
Line 3,818: | Line 3,821: | ||
sleep 10 |
sleep 10 |
||
</lang> |
</lang> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
<lang Red>Red [] |
<lang Red>Red [] |
||
Line 3,979: | Line 3,983: | ||
110 |
110 |
||
111</pre> |
111</pre> |
||
=={{header|S-lang}}== |
=={{header|S-lang}}== |
||
Line 4,056: | Line 4,059: | ||
1111 |
1111 |
||
10000 |
10000 |
||
</pre> |
|||
=={{header|SequenceL}}== |
|||
<lang sequencel>main := toBinaryString([5, 50, 9000]); |
|||
toBinaryString(number(0)) := |
|||
let |
|||
val := "1" when number mod 2 = 1 else "0"; |
|||
in |
|||
toBinaryString(floor(number/2)) ++ val when floor(number/2) > 0 |
|||
else |
|||
val;</lang> |
|||
{{out}} |
|||
<pre> |
|||
["101","110010","10001100101000"] |
|||
</pre> |
</pre> |
||
Line 4,066: | Line 4,085: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
<lang simula>BEGIN |
<lang simula>BEGIN |
||
Line 4,088: | Line 4,108: | ||
10001100101000 |
10001100101000 |
||
</pre> |
</pre> |
||
=={{header|SequenceL}}== |
|||
<lang sequencel>main := toBinaryString([5, 50, 9000]); |
|||
toBinaryString(number(0)) := |
|||
let |
|||
val := "1" when number mod 2 = 1 else "0"; |
|||
in |
|||
toBinaryString(floor(number/2)) ++ val when floor(number/2) > 0 |
|||
else |
|||
val;</lang> |
|||
{{out}} |
|||
<pre> |
|||
["101","110010","10001100101000"] |
|||
</pre> |
|||
=={{header|SkookumScript}}== |
=={{header|SkookumScript}}== |
||
Line 4,138: | Line 4,141: | ||
print (Int.fmt StringCvt.BIN 50 ^ "\n"); |
print (Int.fmt StringCvt.BIN 50 ^ "\n"); |
||
print (Int.fmt StringCvt.BIN 9000 ^ "\n");</lang> |
print (Int.fmt StringCvt.BIN 9000 ^ "\n");</lang> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<lang Swift>for num in [5, 50, 9000] { |
<lang Swift>for num in [5, 50, 9000] { |
||
Line 4,281: | Line 4,285: | ||
0 OK, 0:775</pre> |
0 OK, 0:775</pre> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
<lang sh># Define a function to output binary digits |
<lang sh># Define a function to output binary digits |
||
Line 4,359: | Line 4,364: | ||
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}}== |