Binary digits: Difference between revisions

Add Refal
(Add Refal)
 
(26 intermediate revisions by 12 users not shown)
Line 1,228:
UNTIL N% = 0
=A$</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 for c = 1 to 3
20 read n
30 print n;"-> ";vin$(n)
40 next c
80 end
100 sub vin$(n)
110 b$ = ""
120 n = abs(int(n))
130 '
140 b$ = str$(n mod 2)+b$
150 n = int(n/2)
160 if n > 0 then 130
170 vin$ = b$
180 end sub
200 data 5,50,9000</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
Line 1,390 ⟶ 1,408:
PRINT " -> "; BIN$(9000)
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "binardig"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
DIM a[3]
a[0] = 5
a[1] = 50
a[2] = 9000
FOR i = 0 TO 2
PRINT FORMAT$ ("####", a[i]); " -> "; BIN$(a[i])
NEXT i
 
END FUNCTION
END PROGRAM</syntaxhighlight>
 
=={{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.
Line 2,056 ⟶ 2,095:
9000: 10001100101000
</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc main() void:
writeln(5:b);
writeln(50:b);
writeln(9000:b);
corp</syntaxhighlight>
{{out}}
<pre>101
110010
10001100101000</pre>
 
=={{header|dt}}==
<syntaxhighlight lang="dt">[dup 1 gt? [dup 2 % swap 2 / loop] swap do?] \loop def
 
[\loop doin rev \to-string map "" join] \bin def
 
[0 1 2 5 50 9000] \bin map " " join pl</syntaxhighlight>
{{out}}
<pre>0 1 10 101 110010 10001100101000</pre>
 
=={{header|Dyalect}}==
 
A default <code>ToString</code> method of type <code>Integer</code> is overridenoverridden and returns a binary representation of a number:
 
<syntaxhighlight lang="dyalect">func Integer.ToString() {
Line 2,073 ⟶ 2,133:
print("5 == \(5), 50 = \(50), 1000 = \(9000)")</syntaxhighlight>
 
{{out}}
<pre>5 == 101, 50 = 110010, 1000 = 10001100101000</pre>
 
<pre>5 == 101, 50 = 110010, 1000 = 10001100101000</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
 
func$ bin num .
<syntaxhighlight lang="text">
b$ = ""
func toBinary num . binary$ .
binary$while =num ""> 1
currentNum b$ = num mod 2 & b$
num = num div 2
repeat
binary$ = currentNum mod 2 & binary$
currentNum = currentNum div 2
until currentNum < 2
.
binary$return = currentNumnum & binaryb$
.
print bin 5
call toBinary 5 binary$
print binary$bin 50
print bin 9000
call toBinary 50 binary$
print binary$
call toBinary 9000 binary$
print binary$
</syntaxhighlight>
{{out}}
Line 2,119 ⟶ 2,172:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BinaryDigits {
{
@Inject Console console;
void run() {
Int64[] tests = [0, 1, 5, 50, 9000];
{
Int[] tests = [0, 1, 5, 50, 9000];
 
Int longestInt = tests.map(n -> n.estimateStringLength()).reduce(0, (max, len) -> max.maxOf(len));
Int longestBin = tests.map(n -> (64-n.leadingZeroCount).maxOf(1)) .reduce(0, (max, len) -> max.maxOfnotLessThan(len));
Int longestBin = tests.map(n -> (64-n.leadingZeroCount).notLessThan(1))
.reduce(0, (max, len) -> max.maxOf(len));
 
function String(IntInt64) num = n -> {
{
Int indent = longestInt - n.estimateStringLength();
return $"{' ' * indent}{n}";
};
 
function String(IntInt64) bin = n -> {
{
Int index = n.leadingZeroCount.minOf(63);
Int indent = index - (64 - longestBin);
val bits = n.toBitArray()[index ..< 64];
return $"{' ' * indent}{bits.toString().substring(2)}";
};
 
for (IntInt64 test : tests) {
{
console.print($"The decimal value {num(test)} should produce an output of {bin(test)}");
}
}
}
}
</syntaxhighlight>
 
Line 2,161 ⟶ 2,211:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 2,167 ⟶ 2,217:
public program()
{
new int[]{5,50,9000}.forEach::(n)
{
console.printLine(n.toString(2))
Line 2,178 ⟶ 2,228:
10001100101000
</pre>
 
=={{header|Elixir}}==
Use <code>Integer.to_string</code> with a base of 2:
Line 2,218 ⟶ 2,269:
110010
10001100101000
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun int-to-binary (val)
(let ((x val) (result ""))
(while (> x 0)
(setq result (concat (number-to-string (% x 2)) result))
(setq x (/ x 2)))
result))
 
(message "5 => %s" (int-to-binary 5))
(message "50 => %s" (int-to-binary 50))
(message "9000 => %s" (int-to-binary 9000))
</syntaxhighlight>
{{out}}
<pre>
5 => 101
50 => 110010
9000 => 10001100101000
</pre>
 
Line 2,763 ⟶ 2,834:
</pre>
=={{header|J}}==
Generate a list of binary digits and use it to select characters from string '01'.
<syntaxhighlight lang="j"> tobin=: -.&' '@":@#:
<syntaxhighlight lang="j"> tobin=: '01'{~#:
tobin 5
101
Line 2,770 ⟶ 2,842:
tobin 9000
10001100101000</syntaxhighlight>
Uses implicit output.
Algorithm: Remove spaces from the character list which results from formatting the binary list which represents the numeric argument.
 
I am using implicit output.
=={{header|Java}}==
<p>
<syntaxhighlight lang="java">public class Main {
The <code>Integer</code> class offers the <code>toBinaryString</code> method.
public static void main(String[] args) {
</p>
System.out.println(Integer.toBinaryString(5));
<syntaxhighlight lang="java">
System.out.println(Integer.toBinaryString(50));
System.out.println(Integer.toBinaryString(9000)5);
</syntaxhighlight>
}
}</syntaxhighlight lang="java">
Integer.toBinaryString(50);
{{out}}
</syntaxhighlight>
<pre>101
<syntaxhighlight lang="java">
Integer.toBinaryString(9000);
</syntaxhighlight>
<p>
If you printed these values you would get the following.
</p>
<pre>
101
110010
10001100101000</pre>
</pre>
 
=={{header|JavaScript}}==
===ES5===
Line 2,892 ⟶ 2,973:
9000 -> 一〇〇〇一一〇〇一〇一〇〇〇</pre>
=={{header|Joy}}==
<syntaxhighlight lang="joy">HIDEDEFINE bin == "" [pop 1 >] [[2 div "01" of] dip cons] while ["01" of] dip cons.
 
_ == [null] [pop] [2 div swap] [48 + putch] linrec
[0 1 2 5 50 9000] [bin] map put.</syntaxhighlight>
IN
{{out}}
int2bin == [null] [48 + putch] [_] ifte '\n putch
<pre>["0" "1" "10" "101" "110010" "10001100101000"]</pre>
END</syntaxhighlight>
 
Using int2bin:
<syntaxhighlight lang="joy">0 setautoput
0 int2bin
5 int2bin
50 int2bin
9000 int2bin.</syntaxhighlight>
=={{header|jq}}==
<syntaxhighlight lang="jq">def binary_digits:
Line 3,009 ⟶ 3,085:
-> 101 110010 10001100101000
}
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
fn.println(fn.toTextBase(5, 2))
fn.println(fn.toTextBase(50, 2))
fn.println(fn.toTextBase(9000, 2))
</syntaxhighlight>
 
Line 3,468 ⟶ 3,551:
 
</pre >
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE BINARY
.MCALL .TTYOUT,.EXIT
BINARY::MOV #3$,R5
BR 2$
1$: JSR PC,PRBIN
2$: MOV (R5)+,R0
BNE 1$
.EXIT
3$: .WORD ^D5, ^D50, ^D9000, 0
 
; PRINT R0 AS BINARY WITH NEWLINE
PRBIN: MOV #3$,R1
1$: MOV #'0,R2
ROR R0
ADC R2
MOVB R2,(R1)+
TST R0
BNE 1$
2$: MOVB -(R1),R0
.TTYOUT
BNE 2$
RTS PC
.BYTE 0,0,12,15
3$: .BLKB 16 ; BUFFER
.END BINARY</syntaxhighlight>
{{out}}
<pre>101
110010
10001100101000</pre>
 
=={{header|MAD}}==
MAD has basically no support for runtime generation of strings.
Line 3,498 ⟶ 3,612:
50: 110010
9000: 10001100101000</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
Line 3,589 ⟶ 3,704:
io.write_string(int_to_base_string(N, 2), !IO),
io.nl(!IO).</syntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.1937.30}}
<syntaxhighlight lang="min">(2 over over mod 'div dip) :divmod2
symbol bin
(int :i ==> str :s)
(i (dup 2 <) 'string ('odd? ("1") ("0") if swap 1 shr) 'prefix linrec @s)
) ::
 
(0 1 2 5 50 9000) 'bin map ", " join puts!</syntaxhighlight>
(
{{out}}
:n () =list
<pre>0, 1, 10, 101, 110010, 10001100101000</pre>
(n 0 >) (n divmod2 list append #list @n) while
list reverse 'string map "" join
"^0+" "" replace ;remove leading zeroes
) :bin
 
(5 50 9000) (bin puts) foreach</syntaxhighlight>
{{out}}
<pre>
101
110010
10001100101000
</pre>
=={{header|MiniScript}}==
=== Iterative ===
Line 4,994 ⟶ 5,104:
=={{header|Retro}}==
<syntaxhighlight lang="retro">9000 50 5 3 [ binary putn cr decimal ] times</syntaxhighlight>
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Binary 5>
<Binary 50>
<Binary 9000>>;
};
 
Binary {
0 = '0\n';
s.N = <Binary1 s.N> '\n';
};
 
Binary1 {
0 = ;
s.N, <Divmod s.N 2>: (s.R) s.D = <Binary1 s.R> <Symb s.D>;
};</syntaxhighlight>
{{out}
<pre>101
110010
10001100101000</pre>
 
=={{header|REXX}}==
This version handles the special case of zero simply.
Line 5,073 ⟶ 5,204:
101010111111101001000101110110100000111011011011110111100110100100000100100001111101101110011101000101110110001101101000100100100110000111001010101011110010001111100011110100010101011011111111000110101110111100001011100111110000000010101100110101001010001001001011000000110000010010010100010010000001110100101000011111001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
Line 5,088 ⟶ 5,220:
next
</syntaxhighlight>
 
=={{header|Roc}}==
<syntaxhighlight lang="roc">binstr : Int * -> Str
binstr = \n ->
if n < 2 then
Num.toStr n
else
Str.concat (binstr (Num.shiftRightZfBy n 1)) (Num.toStr (Num.bitwiseAnd n 1))</syntaxhighlight>
 
=={{header|RPL}}==
RPL handles both floating point numbers and binary integers, but the latter are visualized with a <code>#</code> at the beginning and a specific letter at the end identifying the number base, according to the current display mode setting. 42 will then be displayed # 42d, # 2Ah, # 52o or # 101010b depending on the display mode set by resp. <code>DEC</code>, <code>HEX</code>, <code>OCT</code> or <code>BIN</code>.
Line 5,095 ⟶ 5,236:
≪ BIN R→B →STR 3 OVER SIZE 1 - SUB ≫
'→PLNB' STO
{{in}}
 
<pre>
42 →PLNB
5 →PLNB
50 →PLNB
9000 →PLNB
</pre>
{{out}}
<pre>
3: "101"
2A
2: "110010"
1: "10001100101000"
</pre>
 
Line 5,174 ⟶ 5,321:
110010
10001100101000</pre>
 
=={{header|S-BASIC}}==
<syntaxhighlight lang = "BASIC">
 
rem - Return binary representation of n
 
function bin$(n = integer) = string
var s = string
s = ""
while n > 0 do
begin
if (n - (n / 2) * 2) = 0 then
s = "0" + s
else
s = "1" + s
n = n / 2
end
end = s
 
rem - exercise the function
 
print "5 = "; bin$(5)
print "50 = "; bin$(50)
print "9000 = "; bin$(9000)
 
end
</syntaxhighlight>
{{out}}
<pre>
5 = 101
50 = 110010
9000 = 10001100101000
</pre>
 
=={{header|Scala}}==
Scala has an implicit conversion from <code>Int</code> to <code>RichInt</code> which has a method <code>toBinaryString</code>.
Line 5,261 ⟶ 5,442:
10000
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program binary_digits;
loop for n in [5, 50, 9000] do
print(bin n);
end loop;
 
op bin(n);
return reverse +/[str [n mod 2, n div:=2](1) : until n=0];
end op;
end program;</syntaxhighlight>
{{out}}
<pre>101
110010
10001100101000</pre>
 
=={{header|SequenceL}}==
Line 5,277 ⟶ 5,473:
["101","110010","10001100101000"]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">[5, 50, 9000].each { |n|
Line 5,906 ⟶ 6,103:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
System.print("Converting to binary:")
Line 5,918 ⟶ 6,115:
9000 -> 10001100101000
</pre>
 
=={{header|X86 Assembly}}==
Translation of XPL0. Assemble with tasm, tlink /t
2,094

edits