Binary digits: Difference between revisions

Add Refal
(Add emacs lisp)
(Add Refal)
 
(15 intermediate revisions by 8 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 2,077 ⟶ 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,094 ⟶ 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$ = ""
proc toBinary num . binary$ .
binary$while =num ""> 1
if num b$ = 0num mod 2 & b$
binary$ = "0"
.
while num > 0
binary$ = num mod 2 & binary$
num = num div 2
.
return num & b$
.
print bin 5
call toBinary 2 binary$
print binary$bin 50
print bin 9000
call toBinary 50 binary$
print binary$
call toBinary 9000 binary$
print binary$
</syntaxhighlight>
{{out}}
Line 2,140 ⟶ 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,182 ⟶ 2,211:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 2,188 ⟶ 2,217:
public program()
{
new int[]{5,50,9000}.forEach::(n)
{
console.printLine(n.toString(2))
Line 2,199 ⟶ 2,228:
10001100101000
</pre>
 
=={{header|Elixir}}==
Use <code>Integer.to_string</code> with a base of 2:
Line 2,804 ⟶ 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,811 ⟶ 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>
Line 3,521 ⟶ 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,551 ⟶ 3,612:
50: 110010
9000: 10001100101000</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
Line 5,042 ⟶ 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,121 ⟶ 5,204:
101010111111101001000101110110100000111011011011110111100110100100000100100001111101101110011101000101110110001101101000100100100110000111001010101011110010001111100011110100010101011011111111000110101110111100001011100111110000000010101100110101001010001001001011000000110000010010010100010010000001110100101000011111001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
Line 5,136 ⟶ 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,228 ⟶ 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,315 ⟶ 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,331 ⟶ 5,473:
["101","110010","10001100101000"]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">[5, 50, 9000].each { |n|
Line 5,960 ⟶ 6,103:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
System.print("Converting to binary:")
Line 5,972 ⟶ 6,115:
9000 -> 10001100101000
</pre>
 
=={{header|X86 Assembly}}==
Translation of XPL0. Assemble with tasm, tlink /t
2,094

edits