tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,12 @@
}: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:

View file

@ -0,0 +1,4 @@
echo -e "5\n32\n2329" | 0815 bin.0
101
110010
10001100101001

View file

@ -0,0 +1,7 @@
The task is to output the sequence of binary digits for a given [[wp:Natural number|non-negative integer]].
The decimal value <tt>5</tt>, should produce an output of <tt>101</tt>
The decimal value <tt>50</tt> should produce an output of <tt>110010</tt>
The decimal value <tt>9000</tt> should produce an output of <tt>10001100101000</tt>
The results can be achieved using builtin radix functions within the language, if these are available, or alternatively a user defined function can be used. The output produced should consist just of the binary digits of each number followed by a newline. 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.

View file

@ -0,0 +1,4 @@
---
category:
- Radices
note: Basic language learning

View file

@ -0,0 +1,15 @@
(include-book "arithmetic-3/top" :dir :system)
(defun bin-string-r (x)
(if (zp x)
""
(string-append
(bin-string-r (floor x 2))
(if (= 1 (mod x 2))
"1"
"0"))))
(defun bin-string (x)
(if (zp x)
"0"
(bin-string-r x)))

View file

@ -0,0 +1,14 @@
#!/usr/local/bin/a68g --script #
printf((
$g" => "2r3d l$, 5, BIN 5,
$g" => "2r6d l$, 50, BIN 50,
$g" => "2r14d l$, 9000, BIN 9000
));
# or coerce to an array of BOOL #
print((
5, " => ", []BOOL(BIN 5)[bits width-3+1:], new line,
50, " => ", []BOOL(BIN 50)[bits width-6+1:], new line,
9000, " => ", []BOOL(BIN 9000)[bits width-14+1:], new line
))

View file

@ -0,0 +1,23 @@
BEGIN {
print tobinary(5)
print tobinary(50)
print tobinary(9000)
}
function tobinary(num) {
outstr = ""
l = num
while ( l ) {
if ( l%2 == 0 ) {
outstr = "0" outstr
} else {
outstr = "1" outstr
}
l = int(l/2)
}
# Make sure we output a zero for a value of zero
if ( outstr == "" ) {
outstr = "0"
}
return outstr
}

View file

@ -0,0 +1,28 @@
with Ada.Text_IO;
procedure Binary_Output is
package IIO is new Ada.Text_IO.Integer_IO(Integer);
function To_Binary(N: Natural) return String is
S: String(1 .. 1000); -- more than plenty!
Left: Positive := S'First;
Right: Positive := S'Last;
begin
IIO.Put(To => S, Item => N, Base => 2); -- This is the conversion!
-- Now S is a String with many spaces and some "2#...#" somewhere.
-- We only need the "..." part without spaces or base markers.
while S(Left) /= '#' loop
Left := Left + 1;
end loop;
while S(Right) /= '#' loop
Right := Right - 1;
end loop;
return S(Left+1 .. Right-1);
end To_Binary;
begin
Ada.Text_IO.Put_Line(To_Binary(5)); -- 101
Ada.Text_IO.Put_Line(To_Binary(50)); -- 110010
Ada.Text_IO.Put_Line(To_Binary(9000)); -- 10001100101000
end Binary_Output;

View file

@ -0,0 +1,10 @@
MsgBox % NumberToBinary(5) ;101
MsgBox % NumberToBinary(50) ;110010
MsgBox % NumberToBinary(9000) ;10001100101000
NumberToBinary(InputNumber)
{
While, InputNumber
Result := (InputNumber & 1) . Result, InputNumber >>= 1
Return, Result
}

View file

@ -0,0 +1,15 @@
ConsoleWrite(IntToBin(50) & @CRLF)
Func IntToBin($iInt)
$Stack = ObjCreate("System.Collections.Stack")
Local $b = -1, $r = ""
While $iInt <> 0
$b = Mod($iInt, 2)
$iInt = INT($iInt/2)
$Stack.Push ($b)
WEnd
For $i = 1 TO $Stack.Count
$r &= $Stack.Pop
Next
Return $r
EndFunc ;==>IntToBin

View file

@ -0,0 +1,16 @@
FOR num% = 0 TO 16
PRINT FN_tobase(num%, 2, 0)
NEXT
END
REM Convert N% to string in base B% with minimum M% digits:
DEF FN_tobase(N%,B%,M%)
LOCAL D%,A$
REPEAT
D% = N%MODB%
N% DIV= B%
IF D%<0 D% += B%:N% -= 1
A$ = CHR$(48 + D% - 7*(D%>9)) + A$
M% -= 1
UNTIL (N%=FALSE OR N%=TRUE) AND M%<=0
=A$

View file

@ -0,0 +1,12 @@
PRINT FNbinary(5)
PRINT FNbinary(50)
PRINT FNbinary(9000)
END
DEF FNbinary(N%)
LOCAL A$
REPEAT
A$ = STR$(N% AND 1) + A$
N% = N% >>> 1 : REM BBC Basic prior to V5 can use N% = N% DIV 2
UNTIL N% = 0
=A$

View file

@ -0,0 +1,14 @@
@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

View file

@ -0,0 +1,17 @@
+[ Start with n=1 to kick off the loop
[>>++<< Set up {n 0 2} for divmod magic
[->+>- Then
[>+>>]> do
[+[-<+>]>+>>] the
<<<<<<] magic
>>>+ Increment n % 2 so that 0s don't break things
>] Move into n / 2 and divmod that unless it's 0
-< Set up sentinel 1 then move into the first binary digit
[++++++++ ++++++++ ++++++++ Add 47 to get it to ASCII
++++++++ ++++++++ +++++++. and print it
[<]<] Get to a 0; the cell to the left is the next binary digit
>>[<+>-] Tape is {0 n}; make it {n 0}
>[>+] Get to the 1
<[[-]<] Zero the tape for the next iteration
++++++++++. Print a newline
[-]<+] Zero it then increment n and go again

View file

@ -0,0 +1,4 @@
blsq ) {5 50 9000}{2B!}m[uN
101
110010
10001100101000

View file

@ -0,0 +1,25 @@
#include <iostream>
#include <bitset>
#include <string>
#include <climits>
void print_bin(int n)
{
// convert to binary, then to string
std::string str = std::bitset<CHAR_BIT * sizeof n>(n).to_string();
// trim leading zeroes
if(n == 0)
str = "0";
else
str = str.substr(str.find('1'));
// output
std::cout << str << '\n';
}
int main()
{
print_bin(0);
print_bin(5);
print_bin(50);
print_bin(9000);
}

View file

@ -0,0 +1,20 @@
#include <stdio.h>
void bin(int x, char *s)
{
char*_(int x){
*(s = x ? _(x >> 1) : s) = (x & 1) + '0';
return ++s;
}
*_(x) = 0;
}
int main()
{
char a[100];
int i;
for (i = 0; i <= 1984; i += 31)
bin(i, a), printf("%4d: %s\n", i, a);
return 0;
}

View file

@ -0,0 +1,30 @@
#include <stdio.h>
void IntToBitString(unsigned int number)
{
int num_bits = sizeof(unsigned int) * 8;
bool startPrinting = false;
for (int bit_pos=num_bits-1; bit_pos >= 0; bit_pos--)
{
bool isBitSet = (number & (1<<bit_pos)) != 0;
if (!startPrinting && isBitSet)
startPrinting = true;
if (startPrinting || bit_pos==0)
printf("%s", isBitSet ? "1":"0");
}
printf("\r\n");
}
int main()
{
IntToBitString(0);
IntToBitString(5);
IntToBitString(50);
IntToBitString(9000);
return 0;
}

View file

@ -0,0 +1,26 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 binary_number pic X(21).
01 str pic X(21).
01 binary_digit pic X.
01 digit pic 9.
01 n pic 9(7).
01 nstr pic X(7).
PROCEDURE DIVISION.
accept nstr
move nstr to n
perform until n equal 0
divide n by 2 giving n remainder digit
move digit to binary_digit
string binary_digit DELIMITED BY SIZE
binary_number DELIMITED BY SPACE
into str
move str to binary_number
end-perform.
display binary_number
stop run.

View file

@ -0,0 +1,3 @@
(Integer/toBinaryString 5)
(Integer/toBinaryString 50)
(Integer/toBinaryString 9000)

View file

@ -0,0 +1,4 @@
binary = (n) ->
new Number(n).toString(2)
console.log binary n for n in [5, 50, 9000]

View file

@ -0,0 +1 @@
(format t "~b" 5)

View file

@ -0,0 +1,6 @@
import std.stdio;
void main() {
foreach (i; 0 .. 16)
writefln("%b", i);
}

View file

@ -0,0 +1,25 @@
String binary(int n) {
if(n<0)
throw new IllegalArgumentException("negative numbers require 2s complement");
if(n==0) return "0";
String res="";
while(n>0) {
res=(n%2).toString()+res;
n=(n/2).toInt();
}
return res;
}
main() {
print(binary(0));
print(binary(1));
print(binary(5));
print(binary(10));
print(binary(50));
print(binary(9000));
print(binary(65535));
print(binary(0xaa5511ff));
print(binary(0x123456789abcde));
// fails due to precision limit
print(binary(0x123456789abcdef));
}

View file

@ -0,0 +1,18 @@
procedure Binary_Digits;
function IntToBinStr(AInt : integer) : string;
begin
Result := '';
while AInt > 0 do
begin
Result := Chr(Ord('0')+(AInt mod 2))+Result;
AInt := AInt div 2;
end;
end;
begin
writeln(' 5: '+IntToBinStr(5));
writeln(' 50: '+IntToBinStr(50));
writeln('9000: '+IntToBinStr(9000));
end;

View file

@ -0,0 +1,11 @@
#define std'dictionary'*.
#define ext'convertors'*.
// --- Program ---
#symbol Program =
[
'program'output write &numeric:5 &radix:2 &:eintformatter << "%n".
'program'output write &numeric:50 &radix:2 &:eintformatter << "%n".
'program'output write &numeric:9000 &radix:2 &:eintformatter << "%n".
].

View file

@ -0,0 +1 @@
lists:map( fun(N) -> io:fwrite("~.2B~n", [N]) end, [5, 50, 9000]).

View file

@ -0,0 +1,13 @@
function toBinary(integer i)
sequence s
s = {}
while i do
s = prepend(s, '0'+and_bits(i,1))
i = floor(i/2)
end while
return s
end function
puts(1, toBinary(5) & '\n')
puts(1, toBinary(50) & '\n')
puts(1, toBinary(9000) & '\n')

View file

@ -0,0 +1,5 @@
USING: io kernel math math.parser ;
5 >bin print
50 >bin print
9000 >bin print

View file

@ -0,0 +1,4 @@
9000 50 5
2 base !
. . .
decimal

View file

@ -0,0 +1,11 @@
package main
import (
"fmt"
)
func main() {
for i := 0; i < 16; i++ {
fmt.Printf("%b\n", i)
}
}

View file

@ -0,0 +1,7 @@
print '''
n binary
----- ---------------
'''
[5, 50, 9000].each {
printf('%5d %15s\n', it, Integer.toBinaryString(it))
}

View file

@ -0,0 +1,16 @@
import Data.List
import Numeric
import Text.Printf
-- Use the built-in function showIntAtBase.
toBin n = showIntAtBase 2 ("01" !!) n ""
-- Implement our own version.
toBin' 0 = []
toBin' x = (toBin' $ x `div` 2) ++ (show $ x `mod` 2)
printToBin n = putStrLn $ printf "%4d %14s %14s" n (toBin n) (toBin' n)
main = do
putStrLn $ printf "%4s %14s %14s" "N" "toBin" "toBin'"
mapM_ printToBin [5, 50, 9000]

View file

@ -0,0 +1,23 @@
procedure main()
every i := 5 | 50 | 255 | 1285 | 9000 do
write(i," = ",binary(i))
end
procedure binary(n) #: return bitstring for integer n
static CT, cm, cb
initial {
CT := table() # cache table for results
cm := 2 ^ (cb := 4) # (tunable) cache modulus & pad bits
}
b := "" # build reversed bit string
while n > 0 do { # use cached result ...
if not (b ||:= \CT[1(i := n % cm, n /:= cm) ]) then {
CT[j := i] := "" # ...or start new cache entry
while j > 0 do
CT[i] ||:= "01"[ 1(1+j % 2, j /:= 2 )]
b ||:= CT[i] := left(CT[i],cb,"0") # finish cache with padding
}
}
return reverse(trim(b,"0")) # nothing extraneous
end

View file

@ -0,0 +1,7 @@
tobin=: -.&' '@":@#:
tobin 5
101
tobin 50
110010
tobin 9000
10001100101000

View file

@ -0,0 +1,7 @@
public class Main {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(5));
System.out.println(Integer.toBinaryString(50));
System.out.println(Integer.toBinaryString(9000));
}
}

View file

@ -0,0 +1,7 @@
function toBinary(number) {
return new Number(number).toString(2);
}
var demoValues = [5, 50, 9000];
for (var i=0; i<demoValues.length; ++i) {
print(toBinary(demoValues[i])); // alert() in a browser, wscript.echo in WSH, etc.
}

View file

@ -0,0 +1,5 @@
HIDE
_ == [null] [pop] [2 div swap] [48 + putch] linrec
IN
int2bin == [null] [48 + putch] [_] ifte '\n putch
END

View file

@ -0,0 +1,5 @@
0 setautoput
0 int2bin
5 int2bin
50 int2bin
9000 int2bin.

View file

@ -0,0 +1,5 @@
tobin: ,/$2_vs
tobin' 5 50 9000
("101"
"110010"
"10001100101000")

View file

@ -0,0 +1,19 @@
HAI 1.3
HOW IZ I binary YR num
I HAS A digit, I HAS A bin ITZ ""
IM IN YR binarizer
digit R MOD OF num AN 2
bin R SMOOSH digit bin MKAY
num R QUOSHUNT OF num AN 2
NOT num, O RLY?
YA RLY, FOUND YR bin
OIC
IM OUTTA YR binarizer
IF U SAY SO
IM IN YR printer UPPIN YR num
VISIBLE I IZ binary YR num MKAY
IM OUTTA YR printer
KTHXBYE

View file

@ -0,0 +1,2 @@
'%b '__number_format set
[5 50 9000] [3 1] reshape .

View file

@ -0,0 +1,15 @@
for a = 0 to 16
print a;"=";dec2bin$(a)
next
a=50:print a;"=";dec2bin$(a)
a=254:print a;"=";dec2bin$(a)
a=9000:print a;"=";dec2bin$(a)
wait
function dec2bin$(num)
if num=0 then dec2bin$="0":exit function
while num>0
dec2bin$=str$(num mod 2)+dec2bin$
num=int(num/2)
wend
end function

View file

@ -0,0 +1,3 @@
10 PRINT BIN$(5)
20 PRINT BIN$(50)
30 PRINT BIN$(9000)

View file

@ -0,0 +1,3 @@
dec2bin(5)
dec2bin(50)
dec2bin(9000)

View file

@ -0,0 +1,4 @@
> printf( "%d\n", convert( 50, 'binary' ) );
110010
> printf( "%d\n", convert( 9000, 'binary' ) );
10001100101000

View file

@ -0,0 +1 @@
StringJoin @@ ToString /@ IntegerDigits[50, 2]

View file

@ -0,0 +1,15 @@
digits([arg]) := block(
[n: first(arg), b: if length(arg) > 1 then second(arg) else 10, v: [ ], q],
if n = 0 then [0] else (
while n # 0 do (
[n, q]: divide(n, b),
v: endcons(q, v)
),
v
)
)$
binary(n) := simplode(reverse(digits(9000, 2)))$
binary(9000);
"10001100101000"

View file

@ -0,0 +1,17 @@
:- module binary_digits.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, list, string.
main(!IO) :-
list.foldl(print_binary_digits, [5, 50, 9000], !IO).
:- pred print_binary_digits(int::in, io::di, io::uo) is det.
print_binary_digits(N, !IO) :-
io.write_string(int_to_base_string(N, 2), !IO),
io.nl(!IO).

View file

@ -0,0 +1,11 @@
MODULE Binary EXPORTS Main;
IMPORT IO, Fmt;
VAR num := 10;
BEGIN
IO.Put(Fmt.Int(num, 2) & "\n");
num := 150;
IO.Put(Fmt.Int(num, 2) & "\n");
END Binary.

View file

@ -0,0 +1,12 @@
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
nums = [5, 50, 9000]
loop n_ = 0 to nums.length - 1
v_ = nums[n_]
say v_.d2x.x2b.strip('L', 0)
end n_
return

View file

@ -0,0 +1,12 @@
let bin_of_int d =
if d < 0 then invalid_arg "bin_of_int" else
if d = 0 then "0" else
let rec aux acc d =
if d = 0 then acc else
aux (string_of_int (d land 1) :: acc) (d lsr 1)
in
String.concat "" (aux [] d)
let () =
let d = read_int () in
Printf.printf "%8s\n" (bin_of_int d)

View file

@ -0,0 +1,9 @@
bundle Default {
class Binary {
function : Main(args : String[]) ~ Nil {
5->ToBinaryString()->PrintLine();
50->ToBinaryString()->PrintLine();
9000->ToBinaryString()->PrintLine();
}
}
}

View file

@ -0,0 +1,42 @@
function BinaryBits(sys n) as string
string buf=nuls 32
sys p=strptr buf
sys le
mov eax,n
mov edi,p
mov ecx,32
'
'STRIP LEADING ZEROS
(
dec ecx
jl fwd done
shl eax,1
jnc repeat
)
'PLACE DIGITS
'
mov byte [edi],49 '1'
inc edi
(
cmp ecx,0
jle exit
mov dl,48 '0'
shl eax,1
(
jnc exit
mov dl,49 '1'
)
mov [edi],dl
inc edi
dec ecx
repeat
)
done:
'
sub edi,p
mov le,edi
if le then return left buf,le
return "0"
end function
print BinaryBits 0xaa 'result 10101010

View file

@ -0,0 +1 @@
bin(n:int)=concat(apply(s->Str(s),binary(n)))

View file

@ -0,0 +1,4 @@
<?php
echo decbin(5);
echo decbin(50);
echo decbin(9000);

View file

@ -0,0 +1 @@
put list (25) (B);

View file

@ -0,0 +1,7 @@
declare text character (50) initial (' ');
put string(text) edit (25) (b);
put skip list (trim(text, '0'));
put string(text) edit (2147483647) (b);
put skip list (trim(text, '0'));

View file

@ -0,0 +1 @@
say .fmt("%b") for 5, 50, 9000;

View file

@ -0,0 +1,3 @@
for (5, 50, 9000) {
printf "%b\n", $_;
}

View file

@ -0,0 +1,8 @@
: (bin 5)
-> "101"
: (bin 50)
-> "110010"
: (bin 9000)
-> "10001100101000"

View file

@ -0,0 +1 @@
@(5,50,900) | foreach-object { [Convert]::ToString($_,2) }

View file

@ -0,0 +1,8 @@
If OpenConsole()
PrintN(Bin(5)) ;101
PrintN(Bin(50)) ;110010
PrintN(Bin(9000)) ;10001100101000
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,18 @@
>>> for i in range(16): print('{0:b}'.format(i))
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111

View file

@ -0,0 +1,18 @@
>>> for i in range(16): print(bin(i))
0b0
0b1
0b10
0b11
0b100
0b101
0b110
0b111
0b1000
0b1001
0b1010
0b1011
0b1100
0b1101
0b1110
0b1111

View file

@ -0,0 +1,20 @@
>>> 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'
>>> for i in range(16): print(bin(i))
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111

View file

@ -0,0 +1,13 @@
/*REXX program demonstrates converting decimal ───► binary. */
numeric digits 1000
x.1 = 0
x.2 = 5
x.3 = 50
x.4 = 9000
do j=1 for 4
y = x2b(d2x(x.j)) + 0
say right(x.j,20) 'decimal, and in binary:' y
end /*j*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,13 @@
/*REXX program demonstrates converting decimal ───► binary. */
x.1 = 0
x.2 = 5
x.3 = 50
x.4 = 9000
do j=1 for 4
y = strip( x2b( d2x( x.j )), 'L', 0)
if y=='' then y=0
say right(x.j,20) 'decimal, and in binary:' y
end /*j*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,11 @@
/*REXX program demonstrates converting decimal ───► binary. */
x.1 = 0
x.2 = 5
x.3 = 50
x.4 = 9000
do j=1 for 4
y = word( strip( x2b( d2x( x.j )), 'L', 0) 0, 1)
say right(x.j,20) 'decimal, and in binary:' y
end /*j*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,3 @@
#lang racket
(for ([i 16])
(displayln (number->string i 2)))

View file

@ -0,0 +1 @@
9000 50 5 3 [ binary putn cr decimal ] times

View file

@ -0,0 +1,3 @@
[5,50,9000].each do |n|
puts "%b" % n
end

View file

@ -0,0 +1,3 @@
for n in [5,50,9000]
puts n.to_s(2)
end

View file

@ -0,0 +1,4 @@
16.times do |i|
puts '%b' % i
puts i.to_s 2
end

View file

@ -0,0 +1,14 @@
input "Number to convert:";a
while 2^(n+1) < a
n = n + 1
wend
for i = n to 0 step -1
x = 2^i
if a >= x then
print 1;
a = a - x
else
print 0;
end if
next

View file

@ -0,0 +1,3 @@
5 toBinaryString // 101
50 toBinaryString // 110010
9000 toBinaryString // 10001100101000

View file

@ -0,0 +1,3 @@
(display (number->string 5 2)) (newline)
(display (number->string 50 2)) (newline)
(display (number->string 9000 2)) (newline)

View file

@ -0,0 +1,10 @@
$ include "seed7_05.s7i";
const proc: main is func
local
var integer: number is 0;
begin
for number range 0 to 16 do
writeln(str(number, 2));
end for;
end func;

View file

@ -0,0 +1,3 @@
5 printOn: Stdout radix:2
50 printOn: Stdout radix:2
9000 printOn: Stdout radix:2

View file

@ -0,0 +1 @@
#(5 50 9000) do:[:each | each printOn: Stdout radix:2. Stdout cr]

View file

@ -0,0 +1,3 @@
print (Int.fmt StringCvt.BIN 5 ^ "\n");
print (Int.fmt StringCvt.BIN 50 ^ "\n");
print (Int.fmt StringCvt.BIN 9000 ^ "\n");

View file

@ -0,0 +1,19 @@
PROGRAM:BINARY
:Disp "NUMBER TO"
:Disp "CONVERT:"
:Input A
:0→N
:0→B
:While 2^(N+1)≤A
:N+1→N
:End
:While N≥0
:iPart(A/2^N)→C
:10^(N)*C+B→B
:If C=1
:Then
:A-2^N→A
:End
:N-1→N
:End
:Disp B

View file

@ -0,0 +1,9 @@
PROGRAM:BINARY
:Input X
:" "→Str1
:Repeat X=0
:X/2→X
:sub("01",2fPart(X)+1,1)+Str1→Str1
:iPart(X)→X
:End
:Str1

View file

@ -0,0 +1,5 @@
PROGRAM:BINARY
:Disp "NUMBER TO"
:Disp "CONVERT"
:Input "Str1"
:Disp real(25,Str1,10,2)

View file

@ -0,0 +1,6 @@
proc num2bin num {
# Convert to _fixed width_ big-endian 32-bit binary
binary scan [binary format "I" $num] "B*" binval
# Strip useless leading zeros by reinterpreting as a big decimal integer
scan $binval "%lld"
}

View file

@ -0,0 +1,7 @@
for {set x 0} {$x < 16} {incr x} {
puts [num2bin $x]
}
puts "--------------"
puts [num2bin 5]
puts [num2bin 50]
puts [num2bin 9000]

View file

@ -0,0 +1,9 @@
# Define a function to output binary digits
tobinary() {
# We use the bench calculator for our conversion
echo "obase=2;$1"|bc
}
# Call the function with each of our values
tobinary 5
tobinary 50

View file

@ -0,0 +1,17 @@
repeat (ALL) {
#10 = Get_Num("Give a numeric value, -1 to end: ", STATLINE)
if (#10 < 0) { break }
Call("BINARY")
Update()
}
return
:BINARY:
do {
Num_Ins(#10 & 1, LEFT+NOCR)
#10 = #10 >> 1
Char(-1)
} while (#10 > 0)
EOL
Ins_Newline
Return

View file

@ -0,0 +1,5 @@
Sub Main()
Console.WriteLine("5: " & Convert.ToString(5, 2))
Console.WriteLine("50: " & Convert.ToString(50, 2))
Console.WriteLine("9000: " & Convert.ToString(9000, 2))
End Sub

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,33 @@
push 0
; Increment indefinitely.
0:
push -1 ; Sentinel value so the printer knows when to stop.
copy 1
call 1
push 10
ochr
push 1
add
jump 0
; Get the binary digits on the stack in reverse order.
1:
dup
push 2
mod
swap
push 2
div
push 0
copy 1
sub
jn 1
pop
; Print them.
2:
dup
jn 3 ; Stop at the sentinel.
onum
jump 2
3:
pop
ret

View file

@ -0,0 +1,29 @@
.model tiny
.code
.486
org 100h
start: mov ax, 5
call binout
call crlf
mov ax, 50
call binout
call crlf
mov ax, 9000
call binout
crlf: mov al, 0Dh ;new line
int 29h
mov al, 0Ah
int 29h
ret
binout: push ax
shr ax, 1
je bo10
call binout
bo10: pop ax
and al, 01h
or al, '0'
int 29h ;display character
ret
end start

View file

@ -0,0 +1,17 @@
include c:\cxpl\codes; \intrinsic code declarations
proc BinOut(N); \Output N in binary
int N;
int R;
[R:= N&1;
N:= N>>1;
if N then BinOut(N);
ChOut(0, R+^0);
];
int I;
[I:= 0;
repeat BinOut(I); CrLf(0);
I:= I+1;
until KeyHit or I=0;
]

View file

@ -0,0 +1,14 @@
10 LET n=5: GO SUB 1000: PRINT s$
20 LET n=50: GO SUB 1000: PRINT s$
30 LET n=9000: GO SUB 1000: PRINT s$
999 STOP
1000 REM convert to binary
1010 LET t=n: REM temporary variable
1020 LET s$="": REM this will contain our binary digits
1030 LET sf=0: REM output has not started yet
1040 FOR l=126 TO 0 STEP -1
1050 LET d$="0": REM assume next digit is zero
1060 IF t>=(2^l) THEN LET d$="1": LET t=t-(2^l): LET sf=1
1070 IF (sf <> 0) THEN LET s$=s$+d$
1080 NEXT l
1090 RETURN