Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Binary-digits/00-META.yaml
Normal file
5
Task/Binary-digits/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Radices
|
||||
from: http://rosettacode.org/wiki/Binary_digits
|
||||
note: Basic language learning
|
||||
13
Task/Binary-digits/00-TASK.txt
Normal file
13
Task/Binary-digits/00-TASK.txt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
;Task:
|
||||
Create and display the sequence of binary digits for a given [[wp:Natural number|non-negative integer]].
|
||||
|
||||
The decimal value '''5''' should produce an output of '''101'''
|
||||
The decimal value '''50''' should produce an output of '''110010'''
|
||||
The decimal value '''9000''' should produce an output of '''10001100101000'''
|
||||
|
||||
The results can be achieved using built-in 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.
|
||||
<br><br>
|
||||
12
Task/Binary-digits/0815/binary-digits-1.0815
Normal file
12
Task/Binary-digits/0815/binary-digits-1.0815
Normal 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:
|
||||
4
Task/Binary-digits/0815/binary-digits-2.0815
Normal file
4
Task/Binary-digits/0815/binary-digits-2.0815
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
echo -e "5\n32\n2329" | 0815 bin.0
|
||||
101
|
||||
110010
|
||||
10001100101001
|
||||
2
Task/Binary-digits/11l/binary-digits.11l
Normal file
2
Task/Binary-digits/11l/binary-digits.11l
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
L(n) [0, 5, 50, 9000]
|
||||
print(‘#4 = #.’.format(n, bin(n)))
|
||||
43
Task/Binary-digits/360-Assembly/binary-digits.360
Normal file
43
Task/Binary-digits/360-Assembly/binary-digits.360
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
* Binary digits 27/08/2015
|
||||
BINARY CSECT
|
||||
USING BINARY,R12
|
||||
LR R12,R15 set base register
|
||||
BEGIN LA R10,4
|
||||
LA R9,N
|
||||
LOOPN MVC W,0(R9)
|
||||
MVI FLAG,X'00'
|
||||
LA R8,32
|
||||
LA R2,CBIN
|
||||
LOOP TM W,B'10000000' test fist bit
|
||||
BZ ZERO zero
|
||||
MVI FLAG,X'01' one written
|
||||
MVI 0(R2),C'1' write 1
|
||||
B CONT
|
||||
ZERO CLI FLAG,X'01' is one written ?
|
||||
BNE BLANK
|
||||
MVI 0(R2),C'0' write 0
|
||||
B CONT
|
||||
BLANK BCTR R2,0 backspace
|
||||
CONT L R3,W
|
||||
SLL R3,1 shilf left
|
||||
ST R3,W
|
||||
LA R2,1(R2) next bit
|
||||
BCT R8,LOOP loop on bits
|
||||
PRINT CLI FLAG,X'00' is '0'
|
||||
BNE NOTZERO
|
||||
MVI 0(R2),C'0' then write 0
|
||||
NOTZERO L R1,0(R9)
|
||||
XDECO R1,CDEC
|
||||
XPRNT CDEC,45
|
||||
LA R9,4(R9)
|
||||
BCT R10,LOOPN loop on numbers
|
||||
RETURN XR R15,R15 set return code
|
||||
BR R14 return to caller
|
||||
N DC F'0',F'5',F'50',F'9000'
|
||||
W DS F work
|
||||
FLAG DS X flag for trailing blanks
|
||||
CDEC DS CL12 decimal value
|
||||
DC C' '
|
||||
CBIN DC CL32' ' binary value
|
||||
YREGS
|
||||
END BINARY
|
||||
90
Task/Binary-digits/6502-Assembly/binary-digits.6502
Normal file
90
Task/Binary-digits/6502-Assembly/binary-digits.6502
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
; C64 - Binary digits
|
||||
; http://rosettacode.org/wiki/Binary_digits
|
||||
|
||||
; *** labels ***
|
||||
|
||||
declow = $fb
|
||||
dechigh = $fc
|
||||
binstrptr = $fd ; $fe is used for the high byte of the address
|
||||
chkcom = $aefd
|
||||
frmnum = $ad8a
|
||||
getadr = $b7f7
|
||||
strout = $ab1e
|
||||
|
||||
; *** main ***
|
||||
|
||||
*=$033c ; sys828 tbuffer ($033c-$03fb)
|
||||
|
||||
jsr chkcom ; check for and skip comma
|
||||
jsr frmnum ; evaluate numeric expression
|
||||
jsr getadr ; convert floating point number to two-byte int
|
||||
jsr dec2bin ; convert two-byte int to binary string
|
||||
lda #<binstr ; load the address of the binary string - low
|
||||
ldy #>binstr ; high byte
|
||||
jsr skiplz ; skip leading zeros, return an address in a/y
|
||||
; that points to the first "1"
|
||||
jsr strout ; print the result
|
||||
rts
|
||||
|
||||
; *** subroutines ****
|
||||
|
||||
; Converts a 16 bit integer to a binary string.
|
||||
; Input: y - low byte of the integer
|
||||
; a - high byte of the integer
|
||||
; Output: a 16 byte string stored at 'binstr'
|
||||
dec2bin sty declow ; store the two-byte integer
|
||||
sta dechigh
|
||||
lda #<binstr ; store the binary string address on the zero page
|
||||
sta binstrptr
|
||||
lda #>binstr
|
||||
sta binstrptr+1
|
||||
ldx #$01 ; start conversion with the high byte
|
||||
wordloop ldy #$00 ; bit counter
|
||||
byteloop asl declow,x ; shift left, bit 7 is shifted into carry
|
||||
bcs one ; carry set? jump
|
||||
lda #"0" ; a="0"
|
||||
bne writebit
|
||||
one lda #"1" ; a="1"
|
||||
writebit sta (binstrptr),y ; write the digit to the string
|
||||
iny ; y++
|
||||
cpy #$08 ; y==8 all bits converted?
|
||||
bne byteloop ; no -> convert next bit
|
||||
clc ; clear carry
|
||||
lda #$08 ; a=8
|
||||
adc binstrptr ; add 8 to the string address pointer
|
||||
sta binstrptr
|
||||
bcc nooverflow ; address low byte did overflow?
|
||||
inc binstrptr+1 ; yes -> increase the high byte
|
||||
nooverflow dex ; x--
|
||||
bpl wordloop ; x<0? no -> convert the low byte
|
||||
rts ; yes -> conversion finished, return
|
||||
|
||||
; Skip leading zeros.
|
||||
; Input: a - low byte of the byte string address
|
||||
; y - high byte -"-
|
||||
; Output: a - low byte of string start address without leading zeros
|
||||
; y - high byte -"-
|
||||
skiplz sta binstrptr ; store the binary string address on the zero page
|
||||
sty binstrptr+1
|
||||
ldy #$00 ; byte counter
|
||||
skiploop lda (binstrptr),y ; load a byte from the string
|
||||
iny ; y++
|
||||
cpy #$11 ; y==17
|
||||
beq endreached ; yes -> end of string reached without a "1"
|
||||
cmp #"1" ; a=="1"
|
||||
bne skiploop ; no -> take the next byte
|
||||
beq add2ptr ; yes -> jump
|
||||
endreached dey ; move the pointer to the last 0
|
||||
add2ptr clc
|
||||
dey
|
||||
tya ; a=y
|
||||
adc binstrptr ; move the pointer to the first "1" in the string
|
||||
bcc loadhigh ; overflow?
|
||||
inc binstrptr+1 ; yes -> increase high byte
|
||||
loadhigh ldy binstrptr+1
|
||||
rts
|
||||
|
||||
; *** data ***
|
||||
|
||||
binstr .repeat 16, $00 ; reserve 16 bytes for the binary digits
|
||||
.byte $0d, $00 ; newline + null terminator
|
||||
32
Task/Binary-digits/8080-Assembly/binary-digits.8080
Normal file
32
Task/Binary-digits/8080-Assembly/binary-digits.8080
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
bdos: equ 5h ; CP/M system call
|
||||
puts: equ 9h ; Print string
|
||||
org 100h
|
||||
lxi h,5 ; Print value for 5
|
||||
call prbin
|
||||
lxi h,50 ; Print value for 50
|
||||
call prbin
|
||||
lxi h,9000 ; Print value for 9000
|
||||
prbin: call bindgt ; Make binary representation of HL
|
||||
mvi c,puts ; Print it
|
||||
jmp bdos
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;; Return the binary representation of the 16-bit number in HL
|
||||
;;; as a string starting at [DE].
|
||||
bindgt: lxi d,binend ; End of binary string
|
||||
ana a ; Clear carry flag
|
||||
binlp: dcx d ; Previous digit
|
||||
mov a,h ; Shift HL left, LSB into carry flag
|
||||
rar
|
||||
mov h,a
|
||||
mov a,l
|
||||
rar
|
||||
mov l,a
|
||||
mvi a,'0' ; Digit '0' or '1' depending on
|
||||
aci 0 ; status of carry flag.
|
||||
stax d
|
||||
mov a,h ; Is HL 0 now?
|
||||
ora l
|
||||
rz ; Then stop
|
||||
jmp binlp ; Otherwise, do next bit
|
||||
binstr: db '0000000000000000' ; Placeholder for string
|
||||
binend: db 13,10,'$' ; end with \r\n
|
||||
85
Task/Binary-digits/8086-Assembly/binary-digits.8086
Normal file
85
Task/Binary-digits/8086-Assembly/binary-digits.8086
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
.model small
|
||||
.stack 1024
|
||||
.data
|
||||
|
||||
TestData0 byte 5,255 ;255 is the terminator
|
||||
TestData1 byte 5,0,255
|
||||
TestData2 byte 9,0,0,0,255
|
||||
|
||||
.code
|
||||
|
||||
start:
|
||||
|
||||
mov ax,@data
|
||||
mov ds,ax
|
||||
|
||||
cld ;String functions are set to auto-increment
|
||||
|
||||
mov ax,2 ;clear screen by setting video mode to 0
|
||||
int 10h ;select text mode - We're already in it, so this clears the screen
|
||||
|
||||
|
||||
mov si,offset TestData0
|
||||
call PrintBinary_NoLeadingZeroes
|
||||
|
||||
mov si,offset TestData1
|
||||
call PrintBinary_NoLeadingZeroes
|
||||
|
||||
mov si,offset TestData2
|
||||
call PrintBinary_NoLeadingZeroes
|
||||
|
||||
ExitDOS:
|
||||
mov ax,4C00h ;return to dos
|
||||
int 21h
|
||||
|
||||
PrintBinary_NoLeadingZeroes proc
|
||||
;input: DS:SI = seg:offset of a 255-terminated sequence of unpacked BCD digits, stored big-endian
|
||||
;setup
|
||||
mov bx,8000h
|
||||
;bl will be our "can we print zeroes yet" flag.
|
||||
;bh is the "revolving bit mask" - we'll compare each bit to it, then rotate it right once.
|
||||
; It's very handy because it's a self-resetting loop counter as well!
|
||||
|
||||
NextDigit:
|
||||
lodsb
|
||||
cmp al,255
|
||||
je Terminated
|
||||
NextBit:
|
||||
test al,bh ;is the bit we're testing right now set?
|
||||
jz PrintZero
|
||||
;else, print one
|
||||
push ax
|
||||
mov dl,'1' ;31h
|
||||
mov ah,2
|
||||
int 21h ;prints the ascii code in DL
|
||||
pop ax
|
||||
or bl,1 ;set "we've printed a one" flag
|
||||
jmp predicate
|
||||
|
||||
PrintZero:
|
||||
test bl,bl
|
||||
jz predicate
|
||||
push ax
|
||||
mov dl,'0' ;30h
|
||||
mov ah,2
|
||||
int 21h
|
||||
pop ax
|
||||
|
||||
predicate:
|
||||
ror bh,1
|
||||
jnc NextBit
|
||||
;if the carry is set, we've rotated BH back to 10000000b,
|
||||
; so move on to the next digit in that case.
|
||||
jmp NextDigit
|
||||
|
||||
|
||||
Terminated:
|
||||
push ax
|
||||
mov ah,2
|
||||
mov dl,13 ;carriage return
|
||||
int 21h
|
||||
mov dl,10 ;linefeed
|
||||
int 21h
|
||||
pop ax
|
||||
ret
|
||||
PrintBinary_NoLeadingZeroes endp
|
||||
2
Task/Binary-digits/8th/binary-digits.8th
Normal file
2
Task/Binary-digits/8th/binary-digits.8th
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
2 base drop
|
||||
#50 . cr
|
||||
152
Task/Binary-digits/AArch64-Assembly/binary-digits.aarch64
Normal file
152
Task/Binary-digits/AArch64-Assembly/binary-digits.aarch64
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program binarydigit.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
sMessAffBindeb: .asciz "The decimal value "
|
||||
sMessAffBin: .asciz " should produce an output of "
|
||||
szRetourLigne: .asciz "\n"
|
||||
|
||||
/*******************************************/
|
||||
/* Uninitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
sZoneConv: .skip 100
|
||||
sZoneBin: .skip 100
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main: /* entry of program */
|
||||
mov x5,5
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10S
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneBin
|
||||
bl conversion2 // binary conversion and display résult
|
||||
ldr x0,qAdrsZoneBin
|
||||
ldr x0,qAdrsMessAffBindeb
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneConv
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsMessAffBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszRetourLigne
|
||||
bl affichageMess
|
||||
/* other number */
|
||||
mov x5,50
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10S
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneBin
|
||||
bl conversion2 // binary conversion and display résult
|
||||
ldr x0,qAdrsZoneBin
|
||||
ldr x0,qAdrsMessAffBindeb
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneConv
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsMessAffBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszRetourLigne
|
||||
bl affichageMess
|
||||
/* other number */
|
||||
mov x5,-1
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10S
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneBin
|
||||
bl conversion2 // binary conversion and display résult
|
||||
ldr x0,qAdrsZoneBin
|
||||
ldr x0,qAdrsMessAffBindeb
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneConv
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsMessAffBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszRetourLigne
|
||||
bl affichageMess
|
||||
/* other number */
|
||||
mov x5,1
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10S
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneBin
|
||||
bl conversion2 // binary conversion and display résult
|
||||
ldr x0,qAdrsZoneBin
|
||||
ldr x0,qAdrsMessAffBindeb
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneConv
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsMessAffBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsZoneBin
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszRetourLigne
|
||||
bl affichageMess
|
||||
|
||||
|
||||
100: // standard end of the program */
|
||||
mov x0, #0 // return code
|
||||
mov x8, #EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
qAdrsZoneConv: .quad sZoneConv
|
||||
qAdrsZoneBin: .quad sZoneBin
|
||||
qAdrsMessAffBin: .quad sMessAffBin
|
||||
qAdrsMessAffBindeb: .quad sMessAffBindeb
|
||||
qAdrszRetourLigne: .quad szRetourLigne
|
||||
/******************************************************************/
|
||||
/* register conversion in binary */
|
||||
/******************************************************************/
|
||||
/* x0 contains the register */
|
||||
/* x1 contains the address of receipt area */
|
||||
conversion2:
|
||||
stp x2,lr,[sp,-16]! // save registers
|
||||
stp x3,x4,[sp,-16]! // save registers
|
||||
clz x2,x0 // number of left zeros bits
|
||||
mov x3,64
|
||||
sub x2,x3,x2 // number of significant bits
|
||||
strb wzr,[x1,x2] // store 0 final
|
||||
sub x3,x2,1 // position counter of the written character
|
||||
2: // loop
|
||||
tst x0,1 // test first bit
|
||||
lsr x0,x0,#1 // shift right one bit
|
||||
bne 3f
|
||||
mov x4,#48 // bit = 0 => character '0'
|
||||
b 4f
|
||||
3:
|
||||
mov x4,#49 // bit = 1 => character '1'
|
||||
4:
|
||||
strb w4,[x1,x3] // character in reception area at position counter
|
||||
sub x3,x3,#1
|
||||
subs x2,x2,#1 // 0 bits ?
|
||||
bgt 2b // no! loop
|
||||
|
||||
100:
|
||||
ldp x3,x4,[sp],16 // restaur 2 registres
|
||||
ldp x2,lr,[sp],16 // restaur 2 registres
|
||||
ret // retour adresse lr x30
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
15
Task/Binary-digits/ACL2/binary-digits.acl2
Normal file
15
Task/Binary-digits/ACL2/binary-digits.acl2
Normal 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)))
|
||||
14
Task/Binary-digits/ALGOL-68/binary-digits.alg
Normal file
14
Task/Binary-digits/ALGOL-68/binary-digits.alg
Normal 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
|
||||
))
|
||||
18
Task/Binary-digits/ALGOL-M/binary-digits.alg
Normal file
18
Task/Binary-digits/ALGOL-M/binary-digits.alg
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
begin
|
||||
procedure writebin(n);
|
||||
integer n;
|
||||
begin
|
||||
procedure inner(x);
|
||||
integer x;
|
||||
begin
|
||||
if x>1 then inner(x/2);
|
||||
writeon(if x-x/2*2=0 then "0" else "1");
|
||||
end;
|
||||
write(""); % start new line %
|
||||
inner(n);
|
||||
end;
|
||||
|
||||
writebin(5);
|
||||
writebin(50);
|
||||
writebin(9000);
|
||||
end
|
||||
24
Task/Binary-digits/ALGOL-W/binary-digits.alg
Normal file
24
Task/Binary-digits/ALGOL-W/binary-digits.alg
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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.
|
||||
1
Task/Binary-digits/APL/binary-digits-1.apl
Normal file
1
Task/Binary-digits/APL/binary-digits-1.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
base2←2∘⊥⍣¯1
|
||||
1
Task/Binary-digits/APL/binary-digits-2.apl
Normal file
1
Task/Binary-digits/APL/binary-digits-2.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
base2 ← {((⌈2⍟⍵+1)⍴2)⊤⍵}
|
||||
161
Task/Binary-digits/ARM-Assembly/binary-digits.arm
Normal file
161
Task/Binary-digits/ARM-Assembly/binary-digits.arm
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program binarydigit.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1
|
||||
.equ WRITE, 4
|
||||
.equ EXIT, 1
|
||||
/* Initialized data */
|
||||
.data
|
||||
|
||||
sMessAffBin: .ascii "The decimal value "
|
||||
sZoneDec: .space 12,' '
|
||||
.ascii " should produce an output of "
|
||||
sZoneBin: .space 36,' '
|
||||
.asciz "\n"
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main: /* entry of program */
|
||||
push {fp,lr} /* save des 2 registres */
|
||||
mov r0,#5
|
||||
ldr r1,iAdrsZoneDec
|
||||
bl conversion10S @ decimal conversion
|
||||
bl conversion2 @ binary conversion and display résult
|
||||
mov r0,#50
|
||||
ldr r1,iAdrsZoneDec
|
||||
bl conversion10S
|
||||
bl conversion2
|
||||
mov r0,#-1
|
||||
ldr r1,iAdrsZoneDec
|
||||
bl conversion10S
|
||||
bl conversion2
|
||||
mov r0,#1
|
||||
ldr r1,iAdrsZoneDec
|
||||
bl conversion10S
|
||||
bl conversion2
|
||||
|
||||
100: /* standard end of the program */
|
||||
mov r0, #0 @ return code
|
||||
pop {fp,lr} @restaur 2 registers
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
iAdrsZoneDec: .int sZoneDec
|
||||
/******************************************************************/
|
||||
/* register conversion in binary */
|
||||
/******************************************************************/
|
||||
/* r0 contains the register */
|
||||
conversion2:
|
||||
push {r0,lr} /* save registers */
|
||||
push {r1-r5} /* save others registers */
|
||||
ldr r1,iAdrsZoneBin @ address reception area
|
||||
clz r2,r0 @ number of left zeros bits
|
||||
rsb r2,#32 @ number of significant bits
|
||||
mov r4,#' ' @ space
|
||||
add r3,r2,#1 @ position counter in reception area
|
||||
1:
|
||||
strb r4,[r1,r3] @ space in other location of reception area
|
||||
add r3,#1
|
||||
cmp r3,#32 @ end of area ?
|
||||
ble 1b @ no! loop
|
||||
mov r3,r2 @ position counter of the written character
|
||||
2: @ loop
|
||||
lsrs r0,#1 @ shift right one bit with flags
|
||||
movcc r4,#48 @ carry clear => character 0
|
||||
movcs r4,#49 @ carry set => character 1
|
||||
strb r4,[r1,r3] @ character in reception area at position counter
|
||||
sub r3,r3,#1 @
|
||||
subs r2,r2,#1 @ 0 bits ?
|
||||
bgt 2b @ no! loop
|
||||
|
||||
ldr r0,iAdrsZoneMessBin
|
||||
bl affichageMess
|
||||
|
||||
100:
|
||||
pop {r1-r5} /* restaur others registers */
|
||||
pop {r0,lr}
|
||||
bx lr
|
||||
iAdrsZoneBin: .int sZoneBin
|
||||
iAdrsZoneMessBin: .int sMessAffBin
|
||||
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {fp,lr} /* save registres */
|
||||
push {r0,r1,r2,r7} /* save others registres */
|
||||
mov r2,#0 /* counter length */
|
||||
1: /* loop length calculation */
|
||||
ldrb r1,[r0,r2] /* read octet start position + index */
|
||||
cmp r1,#0 /* if 0 its over */
|
||||
addne r2,r2,#1 /* else add 1 in the length */
|
||||
bne 1b /* and loop */
|
||||
/* so here r2 contains the length of the message */
|
||||
mov r1,r0 /* address message in r1 */
|
||||
mov r0,#STDOUT /* code to write to the standard output Linux */
|
||||
mov r7, #WRITE /* code call system "write" */
|
||||
swi #0 /* call systeme */
|
||||
pop {r0,r1,r2,r7} /* restaur others registres */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
bx lr /* return */
|
||||
/***************************************************/
|
||||
/* conversion registre en décimal signé */
|
||||
/***************************************************/
|
||||
/* r0 contient le registre */
|
||||
/* r1 contient l adresse de la zone de conversion */
|
||||
conversion10S:
|
||||
push {fp,lr} /* save des 2 registres frame et retour */
|
||||
push {r0-r5} /* save autres registres */
|
||||
mov r2,r1 /* debut zone stockage */
|
||||
mov r5,#'+' /* par defaut le signe est + */
|
||||
cmp r0,#0 /* nombre négatif ? */
|
||||
movlt r5,#'-' /* oui le signe est - */
|
||||
mvnlt r0,r0 /* et inversion en valeur positive */
|
||||
addlt r0,#1
|
||||
mov r4,#10 /* longueur de la zone */
|
||||
1: /* debut de boucle de conversion */
|
||||
bl divisionpar10 /* division */
|
||||
add r1,#48 /* ajout de 48 au reste pour conversion ascii */
|
||||
strb r1,[r2,r4] /* stockage du byte en début de zone r5 + la position r4 */
|
||||
sub r4,r4,#1 /* position précedente */
|
||||
cmp r0,#0
|
||||
bne 1b /* boucle si quotient different de zéro */
|
||||
strb r5,[r2,r4] /* stockage du signe à la position courante */
|
||||
subs r4,r4,#1 /* position précedente */
|
||||
blt 100f /* si r4 < 0 fin */
|
||||
/* sinon il faut completer le debut de la zone avec des blancs */
|
||||
mov r3,#' ' /* caractere espace */
|
||||
2:
|
||||
strb r3,[r2,r4] /* stockage du byte */
|
||||
subs r4,r4,#1 /* position précedente */
|
||||
bge 2b /* boucle si r4 plus grand ou egal a zero */
|
||||
100: /* fin standard de la fonction */
|
||||
pop {r0-r5} /*restaur des autres registres */
|
||||
pop {fp,lr} /* restaur des 2 registres frame et retour */
|
||||
bx lr
|
||||
|
||||
/***************************************************/
|
||||
/* division par 10 signé */
|
||||
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
|
||||
/* and http://www.hackersdelight.org/ */
|
||||
/***************************************************/
|
||||
/* r0 contient le dividende */
|
||||
/* r0 retourne le quotient */
|
||||
/* r1 retourne le reste */
|
||||
divisionpar10:
|
||||
/* r0 contains the argument to be divided by 10 */
|
||||
push {r2-r4} /* save others registers */
|
||||
mov r4,r0
|
||||
ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */
|
||||
smull r1, r2, r3, r0 /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */
|
||||
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
|
||||
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
|
||||
add r0, r2, r1 /* r0 <- r2 + r1 */
|
||||
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
|
||||
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
|
||||
pop {r2-r4}
|
||||
bx lr /* leave function */
|
||||
.align 4
|
||||
.Ls_magic_number_10: .word 0x66666667
|
||||
14
Task/Binary-digits/AWK/binary-digits.awk
Normal file
14
Task/Binary-digits/AWK/binary-digits.awk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
BEGIN {
|
||||
print tobinary(0)
|
||||
print tobinary(1)
|
||||
print tobinary(5)
|
||||
print tobinary(50)
|
||||
print tobinary(9000)
|
||||
}
|
||||
|
||||
function tobinary(num) {
|
||||
outstr = num % 2
|
||||
while (num = int(num / 2))
|
||||
outstr = (num % 2) outstr
|
||||
return outstr
|
||||
}
|
||||
31
Task/Binary-digits/Action-/binary-digits.action
Normal file
31
Task/Binary-digits/Action-/binary-digits.action
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
PROC PrintBinary(CARD v)
|
||||
CHAR ARRAY a(16)
|
||||
BYTE i=[0]
|
||||
|
||||
DO
|
||||
a(i)=(v&1)+'0
|
||||
i==+1
|
||||
v=v RSH 1
|
||||
UNTIL v=0
|
||||
OD
|
||||
|
||||
DO
|
||||
i==-1
|
||||
Put(a(i))
|
||||
UNTIL i=0
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
CARD ARRAY data=[0 5 50 9000]
|
||||
BYTE i
|
||||
CARD v
|
||||
|
||||
FOR i=0 TO 3
|
||||
DO
|
||||
v=data(i)
|
||||
PrintF("Output for %I is ",v)
|
||||
PrintBinary(v)
|
||||
PutE()
|
||||
OD
|
||||
RETURN
|
||||
13
Task/Binary-digits/Ada/binary-digits.ada
Normal file
13
Task/Binary-digits/Ada/binary-digits.ada
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
with ada.text_io; use ada.text_io;
|
||||
procedure binary is
|
||||
bit : array (0..1) of character := ('0','1');
|
||||
|
||||
function bin_image (n : Natural) return string is
|
||||
(if n < 2 then (1 => bit (n)) else bin_image (n / 2) & bit (n mod 2));
|
||||
|
||||
test_values : array (1..3) of Natural := (5,50,9000);
|
||||
begin
|
||||
for test of test_values loop
|
||||
put_line ("Output for" & test'img & " is " & bin_image (test));
|
||||
end loop;
|
||||
end binary;
|
||||
7
Task/Binary-digits/Aime/binary-digits.aime
Normal file
7
Task/Binary-digits/Aime/binary-digits.aime
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
o_xinteger(2, 0);
|
||||
o_byte('\n');
|
||||
o_xinteger(2, 5);
|
||||
o_byte('\n');
|
||||
o_xinteger(2, 50);
|
||||
o_byte('\n');
|
||||
o_form("/x2/\n", 9000);
|
||||
97
Task/Binary-digits/AppleScript/binary-digits-1.applescript
Normal file
97
Task/Binary-digits/AppleScript/binary-digits-1.applescript
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
---------------------- BINARY STRING -----------------------
|
||||
|
||||
-- showBin :: Int -> String
|
||||
on showBin(n)
|
||||
script binaryChar
|
||||
on |λ|(n)
|
||||
text item (n + 1) of "01"
|
||||
end |λ|
|
||||
end script
|
||||
showIntAtBase(2, binaryChar, n, "")
|
||||
end showBin
|
||||
|
||||
|
||||
--------------------------- TEST ---------------------------
|
||||
on run
|
||||
script
|
||||
on |λ|(n)
|
||||
intercalate(" -> ", {n as string, showBin(n)})
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
return unlines(map(result, {5, 50, 9000}))
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS ---------------------
|
||||
|
||||
-- showIntAtBase :: Int -> (Int -> Char) -> Int -> String -> String
|
||||
on showIntAtBase(base, toChr, n, rs)
|
||||
script showIt
|
||||
on |λ|(nd_, r)
|
||||
set {n, d} to nd_
|
||||
set r_ to toChr's |λ|(d) & r
|
||||
if n > 0 then
|
||||
|λ|(quotRem(n, base), r_)
|
||||
else
|
||||
r_
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
if base ≤ 1 then
|
||||
"error: showIntAtBase applied to unsupported base: " & base as string
|
||||
else if n < 0 then
|
||||
"error: showIntAtBase applied to negative number: " & base as string
|
||||
else
|
||||
showIt's |λ|(quotRem(n, base), rs)
|
||||
end if
|
||||
end showIntAtBase
|
||||
|
||||
|
||||
-- quotRem :: Integral a => a -> a -> (a, a)
|
||||
on quotRem(m, n)
|
||||
{m div n, m mod n}
|
||||
end quotRem
|
||||
|
||||
|
||||
-------------------- GENERICS FOR TEST ---------------------
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
set strJoined to lstText as text
|
||||
set my text item delimiters to dlm
|
||||
return strJoined
|
||||
end intercalate
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
intercalate(linefeed, xs)
|
||||
end unlines
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
-- showBin :: Int -> String
|
||||
on showBin(n)
|
||||
script binaryChar
|
||||
on |λ|(n)
|
||||
text item (n + 1) of "〇一"
|
||||
end |λ|
|
||||
end script
|
||||
showIntAtBase(2, binaryChar, n, "")
|
||||
end showBin
|
||||
15
Task/Binary-digits/AppleScript/binary-digits-3.applescript
Normal file
15
Task/Binary-digits/AppleScript/binary-digits-3.applescript
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
on intToBinary(n)
|
||||
set binary to (n mod 2 div 1) as text
|
||||
set n to n div 2
|
||||
repeat while (n > 0)
|
||||
set binary to ((n mod 2 div 1) as text) & binary
|
||||
set n to n div 2
|
||||
end repeat
|
||||
|
||||
return binary
|
||||
end intToBinary
|
||||
|
||||
display dialog ¬
|
||||
intToBinary(5) & linefeed & ¬
|
||||
intToBinary(50) & linefeed & ¬
|
||||
intToBinary(9000) & linefeed
|
||||
23
Task/Binary-digits/AppleScript/binary-digits-4.applescript
Normal file
23
Task/Binary-digits/AppleScript/binary-digits-4.applescript
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
on intToBinary(n)
|
||||
set binary to ""
|
||||
repeat
|
||||
-- Calculate an integer value whose 8 decimal digits are the same as the low 8 binary digits of n's current value.
|
||||
set binAsDec to (n div 128 mod 2 * 10000000 + n div 64 mod 2 * 1000000 + n div 32 mod 2 * 100000 + ¬
|
||||
n div 16 mod 2 * 10000 + n div 8 mod 2 * 1000 + n div 4 mod 2 * 100 + n div 2 mod 2 * 10 + n mod 2) div 1
|
||||
-- Coerce to text as appropriate, prepend to the output text, and prepare to get another 8 digits or not as necessary.
|
||||
if (n > 255) then
|
||||
set binary to text 2 thru -1 of ((100000000 + binAsDec) as text) & binary
|
||||
set n to n div 256
|
||||
else
|
||||
set binary to (binAsDec as text) & binary
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
|
||||
return binary
|
||||
end intToBinary
|
||||
|
||||
display dialog ¬
|
||||
intToBinary(5) & linefeed & ¬
|
||||
intToBinary(50) & linefeed & ¬
|
||||
intToBinary(9000) & linefeed
|
||||
10
Task/Binary-digits/Applesoft-BASIC/binary-digits.basic
Normal file
10
Task/Binary-digits/Applesoft-BASIC/binary-digits.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
0 N = 5: GOSUB 1:N = 50: GOSUB 1:N = 9000: GOSUB 1: END
|
||||
1 LET N2 = ABS ( INT (N))
|
||||
2 LET B$ = ""
|
||||
3 FOR N1 = N2 TO 0 STEP 0
|
||||
4 LET N2 = INT (N1 / 2)
|
||||
5 LET B$ = STR$ (N1 - N2 * 2) + B$
|
||||
6 LET N1 = N2
|
||||
7 NEXT N1
|
||||
8 PRINT B$
|
||||
9 RETURN
|
||||
3
Task/Binary-digits/Arturo/binary-digits.arturo
Normal file
3
Task/Binary-digits/Arturo/binary-digits.arturo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
print as.binary 5
|
||||
print as.binary 50
|
||||
print as.binary 9000
|
||||
10
Task/Binary-digits/AutoHotkey/binary-digits.ahk
Normal file
10
Task/Binary-digits/AutoHotkey/binary-digits.ahk
Normal 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
|
||||
}
|
||||
15
Task/Binary-digits/AutoIt/binary-digits.autoit
Normal file
15
Task/Binary-digits/AutoIt/binary-digits.autoit
Normal 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
|
||||
11
Task/Binary-digits/Axe/binary-digits.axe
Normal file
11
Task/Binary-digits/Axe/binary-digits.axe
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Lbl BIN
|
||||
.Axe supports 16-bit integers, so 16 digits are enough
|
||||
L₁+16→P
|
||||
0→{P}
|
||||
While r₁
|
||||
P--
|
||||
{(r₁ and 1)▶Hex+3}→P
|
||||
r₁/2→r₁
|
||||
End
|
||||
Disp P,i
|
||||
Return
|
||||
10
Task/Binary-digits/BASIC256/binary-digits.basic
Normal file
10
Task/Binary-digits/BASIC256/binary-digits.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# DecToBin.bas
|
||||
# BASIC256 1.1.4.0
|
||||
|
||||
|
||||
dim a(3) #dimension a 3 element array (a)
|
||||
a = {5, 50, 9000}
|
||||
|
||||
for i = 0 to 2
|
||||
print a[i] + chr(9) + toRadix(a[i],2) # radix (decimal, base2)
|
||||
next i
|
||||
16
Task/Binary-digits/BBC-BASIC/binary-digits-1.basic
Normal file
16
Task/Binary-digits/BBC-BASIC/binary-digits-1.basic
Normal 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$
|
||||
12
Task/Binary-digits/BBC-BASIC/binary-digits-2.basic
Normal file
12
Task/Binary-digits/BBC-BASIC/binary-digits-2.basic
Normal 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$
|
||||
16
Task/Binary-digits/BCPL/binary-digits.bcpl
Normal file
16
Task/Binary-digits/BCPL/binary-digits.bcpl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
get "libhdr"
|
||||
|
||||
let writebin(x) be
|
||||
$( let f(x) be
|
||||
$( if x>1 then f(x>>1)
|
||||
wrch((x & 1) + '0')
|
||||
$)
|
||||
f(x)
|
||||
wrch('*N')
|
||||
$)
|
||||
|
||||
let start() be
|
||||
$( writebin(5)
|
||||
writebin(50)
|
||||
writebin(9000)
|
||||
$)
|
||||
3
Task/Binary-digits/BQN/binary-digits-1.bqn
Normal file
3
Task/Binary-digits/BQN/binary-digits-1.bqn
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Bin ← 2{⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)}
|
||||
|
||||
Bin¨5‿50‿9000
|
||||
1
Task/Binary-digits/BQN/binary-digits-2.bqn
Normal file
1
Task/Binary-digits/BQN/binary-digits-2.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
⟨ ⟨ 1 0 1 ⟩ ⟨ 1 1 0 0 1 0 ⟩ ⟨ 1 0 0 0 1 1 0 0 1 0 1 0 0 0 ⟩ ⟩
|
||||
8
Task/Binary-digits/BaCon/binary-digits.bacon
Normal file
8
Task/Binary-digits/BaCon/binary-digits.bacon
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
' Binary digits
|
||||
OPTION MEMTYPE int
|
||||
INPUT n$
|
||||
IF VAL(n$) = 0 THEN
|
||||
PRINT "0"
|
||||
ELSE
|
||||
PRINT CHOP$(BIN$(VAL(n$)), "0", 1)
|
||||
ENDIF
|
||||
14
Task/Binary-digits/Batch-File/binary-digits.bat
Normal file
14
Task/Binary-digits/Batch-File/binary-digits.bat
Normal 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
|
||||
5
Task/Binary-digits/Bc/binary-digits.bc
Normal file
5
Task/Binary-digits/Bc/binary-digits.bc
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
obase = 2
|
||||
5
|
||||
50
|
||||
9000
|
||||
quit
|
||||
4
Task/Binary-digits/Beads/binary-digits.beads
Normal file
4
Task/Binary-digits/Beads/binary-digits.beads
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
beads 1 program 'Binary Digits'
|
||||
calc main_init
|
||||
loop across:[5, 50, 9000] val:v
|
||||
log to_str(v, base:2)
|
||||
1
Task/Binary-digits/Befunge/binary-digits.bf
Normal file
1
Task/Binary-digits/Befunge/binary-digits.bf
Normal file
|
|
@ -0,0 +1 @@
|
|||
&>0\55+\:2%68>*#<+#8\#62#%/#2:_$>:#,_$@
|
||||
17
Task/Binary-digits/Bracmat/binary-digits.bracmat
Normal file
17
Task/Binary-digits/Bracmat/binary-digits.bracmat
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
( dec2bin
|
||||
= bit bits
|
||||
. :?bits
|
||||
& whl
|
||||
' ( !arg:>0
|
||||
& mod$(!arg,2):?bit
|
||||
& div$(!arg,2):?arg
|
||||
& !bit !bits:?bits
|
||||
)
|
||||
& (str$!bits:~|0)
|
||||
)
|
||||
& 0 5 50 9000 423785674235000123456789:?numbers
|
||||
& whl
|
||||
' ( !numbers:%?dec ?numbers
|
||||
& put$(str$(!dec ":\n" dec2bin$!dec \n\n))
|
||||
)
|
||||
;
|
||||
17
Task/Binary-digits/Brainf---/binary-digits.bf
Normal file
17
Task/Binary-digits/Brainf---/binary-digits.bf
Normal 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
|
||||
4
Task/Binary-digits/Burlesque/binary-digits.blq
Normal file
4
Task/Binary-digits/Burlesque/binary-digits.blq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
blsq ) {5 50 9000}{2B!}m[uN
|
||||
101
|
||||
110010
|
||||
10001100101000
|
||||
22
Task/Binary-digits/C++/binary-digits-1.cpp
Normal file
22
Task/Binary-digits/C++/binary-digits-1.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <bitset>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
void print_bin(unsigned int n) {
|
||||
std::string str = "0";
|
||||
|
||||
if (n > 0) {
|
||||
str = std::bitset<std::numeric_limits<unsigned int>::digits>(n).to_string();
|
||||
str = str.substr(str.find('1')); // remove leading zeros
|
||||
}
|
||||
|
||||
std::cout << str << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
print_bin(0);
|
||||
print_bin(5);
|
||||
print_bin(50);
|
||||
print_bin(9000);
|
||||
}
|
||||
14
Task/Binary-digits/C++/binary-digits-2.cpp
Normal file
14
Task/Binary-digits/C++/binary-digits-2.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
#include <bitset>
|
||||
void printBits(int n) { // Use int like most programming languages.
|
||||
int iExp = 0; // Bit-length
|
||||
while (n >> iExp) ++iExp; // Could use template <log(x)*1.44269504088896340736>
|
||||
for (int at = iExp - 1; at >= 0; at--) // Reverse iter from the bit-length to 0 - msb is at end
|
||||
std::cout << std::bitset<32>(n)[at]; // Show 1's, show lsb, hide leading zeros
|
||||
std::cout << '\n';
|
||||
}
|
||||
int main(int argc, char* argv[]) {
|
||||
printBits(5);
|
||||
printBits(50);
|
||||
printBits(9000);
|
||||
} // for testing with n=0 printBits<32>(0);
|
||||
8
Task/Binary-digits/C++/binary-digits-3.cpp
Normal file
8
Task/Binary-digits/C++/binary-digits-3.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <iostream>
|
||||
int main(int argc, char* argv[]) {
|
||||
unsigned int in[] = {5, 50, 9000}; // Use int like most programming languages
|
||||
for (int i = 0; i < 3; i++) // Use all inputs
|
||||
for (int at = 31; at >= 0; at--) // reverse iteration from the max bit-length to 0, because msb is at the end
|
||||
if (int b = (in[i] >> at)) // skip leading zeros. Start output when significant bits are set
|
||||
std::cout << ('0' + b & 1) << (!at ? "\n": ""); // '0' or '1'. Add EOL if last bit of num
|
||||
}
|
||||
7
Task/Binary-digits/C++/binary-digits-4.cpp
Normal file
7
Task/Binary-digits/C++/binary-digits-4.cpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#include <iostream>
|
||||
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 at = 31; at >= 0; at--) // reverse iteration from the max bit-length to 0, because msb is at the end
|
||||
if (int b = (atoi(argv[i]) >> at)) // skip leading zeros
|
||||
std::cout << ('0' + b & 1) << (!at ? "\n": ""); // '0' or '1'. Add EOL if last bit of num
|
||||
}
|
||||
11
Task/Binary-digits/C++/binary-digits-5.cpp
Normal file
11
Task/Binary-digits/C++/binary-digits-5.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <iostream>
|
||||
|
||||
std::string binary(int n) {
|
||||
return n == 0 ? "" : binary(n >> 1) + std::to_string(n & 1);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
std::cout << binary(std::stoi(argv[i])) << std::endl;
|
||||
}
|
||||
}
|
||||
12
Task/Binary-digits/C-sharp/binary-digits-1.cs
Normal file
12
Task/Binary-digits/C-sharp/binary-digits-1.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
foreach (var number in new[] { 5, 50, 9000 })
|
||||
{
|
||||
Console.WriteLine(Convert.ToString(number, 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Task/Binary-digits/C-sharp/binary-digits-2.cs
Normal file
14
Task/Binary-digits/C-sharp/binary-digits-2.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
|
||||
static string ToBinary(uint x) {
|
||||
if(x == 0) return "0";
|
||||
var bin = new StringBuilder();
|
||||
for(uint mask = (uint)1 << (sizeof(uint)*8 - 1);mask > 0;mask = mask >> 1)
|
||||
bin.Append((mask & x) > 0 ? "1" : "0");
|
||||
return bin.ToString().TrimStart('0');
|
||||
}
|
||||
|
||||
Console.WriteLine(ToBinary(5));
|
||||
Console.WriteLine(ToBinary(50));
|
||||
Console.WriteLine(ToBinary(9000));
|
||||
106
Task/Binary-digits/C/binary-digits-1.c
Normal file
106
Task/Binary-digits/C/binary-digits-1.c
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#define _CRT_SECURE_NO_WARNINGS // turn off panic warnings
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE // enable old-gold POSIX names in MSVS
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
char* bin2str(unsigned value, char* buffer)
|
||||
{
|
||||
// This algorithm is not the fastest one, but is relativelly simple.
|
||||
//
|
||||
// A faster algorithm would be conversion octets to strings by a lookup table.
|
||||
// There is only 2**8 == 256 octets, therefore we would need only 2048 bytes
|
||||
// for the lookup table. Conversion of a 64-bit integers would need 8 lookups
|
||||
// instead 64 and/or/shifts of bits etc. Even more... lookups may be implemented
|
||||
// with XLAT or similar CPU instruction... and AVX/SSE gives chance for SIMD.
|
||||
|
||||
const unsigned N_DIGITS = sizeof(unsigned) * 8;
|
||||
unsigned mask = 1 << (N_DIGITS - 1);
|
||||
char* ptr = buffer;
|
||||
|
||||
for (int i = 0; i < N_DIGITS; i++)
|
||||
{
|
||||
*ptr++ = '0' + !!(value & mask);
|
||||
mask >>= 1;
|
||||
}
|
||||
*ptr = '\0';
|
||||
|
||||
// Remove leading zeros.
|
||||
//
|
||||
for (ptr = buffer; *ptr == '0'; ptr++)
|
||||
;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
char* bin2strNaive(unsigned value, char* buffer)
|
||||
{
|
||||
// This variation of the solution doesn't use bits shifting etc.
|
||||
|
||||
unsigned n, m, p;
|
||||
|
||||
n = 0;
|
||||
p = 1; // p = 2 ** n
|
||||
while (p <= value / 2)
|
||||
{
|
||||
n = n + 1;
|
||||
p = p * 2;
|
||||
}
|
||||
|
||||
m = 0;
|
||||
while (n > 0)
|
||||
{
|
||||
buffer[m] = '0' + value / p;
|
||||
value = value % p;
|
||||
m = m + 1;
|
||||
n = n - 1;
|
||||
p = p / 2;
|
||||
}
|
||||
|
||||
buffer[m + 1] = '\0';
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const unsigned NUMBERS[] = { 5, 50, 9000 };
|
||||
|
||||
const int RADIX = 2;
|
||||
char buffer[(sizeof(unsigned)*8 + 1)];
|
||||
|
||||
// Function itoa is an POSIX function, but it is not in C standard library.
|
||||
// There is no big surprise that Microsoft deprecate itoa because POSIX is
|
||||
// "Portable Operating System Interface for UNIX". Thus it is not a good
|
||||
// idea to use _itoa instead itoa: we lost compatibility with POSIX;
|
||||
// we gain nothing in MS Windows (itoa-without-underscore is not better
|
||||
// than _itoa-with-underscore). The same holds for kbhit() and _kbhit() etc.
|
||||
//
|
||||
for (int i = 0; i < sizeof(NUMBERS) / sizeof(unsigned); i++)
|
||||
{
|
||||
unsigned value = NUMBERS[i];
|
||||
itoa(value, buffer, RADIX);
|
||||
printf("itoa: %u decimal = %s binary\n", value, buffer);
|
||||
}
|
||||
|
||||
// Yeep, we can use a homemade bin2str function. Notice that C is very very
|
||||
// efficient (as "hi level assembler") when bit manipulation is needed.
|
||||
//
|
||||
for (int i = 0; i < sizeof(NUMBERS) / sizeof(unsigned); i++)
|
||||
{
|
||||
unsigned value = NUMBERS[i];
|
||||
printf("bin2str: %u decimal = %s binary\n", value, bin2str(value, buffer));
|
||||
}
|
||||
|
||||
// Another implementation - see above.
|
||||
//
|
||||
for (int i = 0; i < sizeof(NUMBERS) / sizeof(unsigned); i++)
|
||||
{
|
||||
unsigned value = NUMBERS[i];
|
||||
printf("bin2strNaive: %u decimal = %s binary\n", value, bin2strNaive(value, buffer));
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
27
Task/Binary-digits/C/binary-digits-2.c
Normal file
27
Task/Binary-digits/C/binary-digits-2.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
char *bin(uint32_t x);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (size_t i = 0; i < 20; i++) {
|
||||
char *binstr = bin(i);
|
||||
printf("%s\n", binstr);
|
||||
free(binstr);
|
||||
}
|
||||
}
|
||||
|
||||
char *bin(uint32_t x)
|
||||
{
|
||||
size_t bits = (x == 0) ? 1 : log10((double) x)/log10(2) + 1;
|
||||
char *ret = malloc((bits + 1) * sizeof (char));
|
||||
for (size_t i = 0; i < bits ; i++) {
|
||||
ret[bits - i - 1] = (x & 1) ? '1' : '0';
|
||||
x >>= 1;
|
||||
}
|
||||
ret[bits] = '\0';
|
||||
return ret;
|
||||
}
|
||||
17
Task/Binary-digits/CLU/binary-digits.clu
Normal file
17
Task/Binary-digits/CLU/binary-digits.clu
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
binary = proc (n: int) returns (string)
|
||||
bin: string := ""
|
||||
while n > 0 do
|
||||
bin := string$c2s(char$i2c(48 + n // 2)) || bin
|
||||
n := n / 2
|
||||
end
|
||||
return(bin)
|
||||
end binary
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
tests: array[int] := array[int]$[5, 50, 9000]
|
||||
|
||||
for test: int in array[int]$elements(tests) do
|
||||
stream$putl(po, int$unparse(test) || " -> " || binary(test))
|
||||
end
|
||||
end start_up
|
||||
26
Task/Binary-digits/COBOL/binary-digits-1.cobol
Normal file
26
Task/Binary-digits/COBOL/binary-digits-1.cobol
Normal 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.
|
||||
27
Task/Binary-digits/COBOL/binary-digits-2.cobol
Normal file
27
Task/Binary-digits/COBOL/binary-digits-2.cobol
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. binary-conversion.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 binary-number pic X(21).
|
||||
01 digit pic 9.
|
||||
01 n pic 9(7).
|
||||
01 nstr pic X(7).
|
||||
01 ptr pic 99.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
display "Number: " with no advancing.
|
||||
accept nstr.
|
||||
move nstr to n.
|
||||
move zeroes to binary-number.
|
||||
move length binary-number to ptr.
|
||||
perform until n equal 0
|
||||
divide n by 2 giving n remainder digit
|
||||
move digit to binary-number(ptr:1)
|
||||
subtract 1 from ptr
|
||||
if ptr < 1
|
||||
exit perform
|
||||
end-if
|
||||
end-perform.
|
||||
display binary-number.
|
||||
stop run.
|
||||
9
Task/Binary-digits/Ceylon/binary-digits.ceylon
Normal file
9
Task/Binary-digits/Ceylon/binary-digits.ceylon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
shared void run() {
|
||||
|
||||
void printBinary(Integer integer) =>
|
||||
print(Integer.format(integer, 2));
|
||||
|
||||
printBinary(5);
|
||||
printBinary(50);
|
||||
printBinary(9k);
|
||||
}
|
||||
3
Task/Binary-digits/Clojure/binary-digits.clj
Normal file
3
Task/Binary-digits/Clojure/binary-digits.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(Integer/toBinaryString 5)
|
||||
(Integer/toBinaryString 50)
|
||||
(Integer/toBinaryString 9000)
|
||||
4
Task/Binary-digits/CoffeeScript/binary-digits.coffee
Normal file
4
Task/Binary-digits/CoffeeScript/binary-digits.coffee
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
binary = (n) ->
|
||||
new Number(n).toString(2)
|
||||
|
||||
console.log binary n for n in [5, 50, 9000]
|
||||
14
Task/Binary-digits/Commodore-BASIC/binary-digits.basic
Normal file
14
Task/Binary-digits/Commodore-BASIC/binary-digits.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
10 READ N
|
||||
20 IF N < 0 THEN 70
|
||||
30 GOSUB 100
|
||||
40 PRINT N"-> "B$
|
||||
50 GOTO 10
|
||||
60 DATA 5, 50, 9000, -1
|
||||
70 END
|
||||
90 REM *** SUBROUTINE: CONVERT INTEGER IN N TO BINARY STRING B$
|
||||
100 B$=""
|
||||
110 FOR N1 = ABS(INT(N)) TO 0 STEP 0
|
||||
120 : B$ = MID$(STR$(N1 AND 1),2) + B$
|
||||
130 : N1 = INT(N1/2)
|
||||
140 NEXT N1
|
||||
150 RETURN
|
||||
5
Task/Binary-digits/Common-Lisp/binary-digits.lisp
Normal file
5
Task/Binary-digits/Common-Lisp/binary-digits.lisp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(format t "~b" 5)
|
||||
|
||||
; or
|
||||
|
||||
(write 5 :base 2)
|
||||
15
Task/Binary-digits/Component-Pascal/binary-digits.pas
Normal file
15
Task/Binary-digits/Component-Pascal/binary-digits.pas
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
MODULE BinaryDigits;
|
||||
IMPORT StdLog,Strings;
|
||||
|
||||
PROCEDURE Do*;
|
||||
VAR
|
||||
str : ARRAY 33 OF CHAR;
|
||||
BEGIN
|
||||
Strings.IntToStringForm(5,2, 1,'0',FALSE,str);
|
||||
StdLog.Int(5);StdLog.String(":> " + str);StdLog.Ln;
|
||||
Strings.IntToStringForm(50,2, 1,'0',FALSE,str);
|
||||
StdLog.Int(50);StdLog.String(":> " + str);StdLog.Ln;
|
||||
Strings.IntToStringForm(9000,2, 1,'0',FALSE,str);
|
||||
StdLog.Int(9000);StdLog.String(":> " + str);StdLog.Ln;
|
||||
END Do;
|
||||
END BinaryDigits.
|
||||
20
Task/Binary-digits/Cowgol/binary-digits.cowgol
Normal file
20
Task/Binary-digits/Cowgol/binary-digits.cowgol
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
sub print_binary(n: uint32) is
|
||||
var buffer: uint8[33];
|
||||
var p := &buffer[32];
|
||||
[p] := 0;
|
||||
|
||||
while n != 0 loop
|
||||
p := @prev p;
|
||||
[p] := ((n as uint8) & 1) + '0';
|
||||
n := n >> 1;
|
||||
end loop;
|
||||
|
||||
print(p);
|
||||
print_nl();
|
||||
end sub;
|
||||
|
||||
print_binary(5);
|
||||
print_binary(50);
|
||||
print_binary(9000);
|
||||
3
Task/Binary-digits/Crystal/binary-digits-1.crystal
Normal file
3
Task/Binary-digits/Crystal/binary-digits-1.crystal
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[5,50,9000].each do |n|
|
||||
puts "%b" % n
|
||||
end
|
||||
1
Task/Binary-digits/Crystal/binary-digits-2.crystal
Normal file
1
Task/Binary-digits/Crystal/binary-digits-2.crystal
Normal file
|
|
@ -0,0 +1 @@
|
|||
{5,50,9000}.each { |n| puts n.to_s(2) }
|
||||
6
Task/Binary-digits/D/binary-digits.d
Normal file
6
Task/Binary-digits/D/binary-digits.d
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
foreach (immutable i; 0 .. 16)
|
||||
writefln("%b", i);
|
||||
}
|
||||
25
Task/Binary-digits/Dart/binary-digits.dart
Normal file
25
Task/Binary-digits/Dart/binary-digits.dart
Normal 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));
|
||||
}
|
||||
1
Task/Binary-digits/Dc/binary-digits.dc
Normal file
1
Task/Binary-digits/Dc/binary-digits.dc
Normal file
|
|
@ -0,0 +1 @@
|
|||
2o 5p 50p 9000p
|
||||
19
Task/Binary-digits/Delphi/binary-digits.delphi
Normal file
19
Task/Binary-digits/Delphi/binary-digits.delphi
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
program BinaryDigit;
|
||||
{$APPTYPE CONSOLE}
|
||||
uses
|
||||
sysutils;
|
||||
|
||||
function IntToBinStr(AInt : LongWord) : string;
|
||||
begin
|
||||
Result := '';
|
||||
repeat
|
||||
Result := Chr(Ord('0')+(AInt and 1))+Result;
|
||||
AInt := AInt div 2;
|
||||
until (AInt = 0);
|
||||
end;
|
||||
|
||||
Begin
|
||||
writeln(' 5: ',IntToBinStr(5));
|
||||
writeln(' 50: ',IntToBinStr(50));
|
||||
writeln('9000: '+IntToBinStr(9000));
|
||||
end.
|
||||
13
Task/Binary-digits/Dyalect/binary-digits.dyalect
Normal file
13
Task/Binary-digits/Dyalect/binary-digits.dyalect
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
func Integer.ToString() {
|
||||
var s = ""
|
||||
for x in 31^-1..0 {
|
||||
if this &&& (1 <<< x) != 0 {
|
||||
s += "1"
|
||||
} else if s != "" {
|
||||
s += "0"
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
print("5 == \(5), 50 = \(50), 1000 = \(9000)")
|
||||
16
Task/Binary-digits/EasyLang/binary-digits.easy
Normal file
16
Task/Binary-digits/EasyLang/binary-digits.easy
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
proc toBinary num . binary$ .
|
||||
binary$ = ""
|
||||
if num = 0
|
||||
binary$ = "0"
|
||||
.
|
||||
while num > 0
|
||||
binary$ = num mod 2 & binary$
|
||||
num = num div 2
|
||||
.
|
||||
.
|
||||
call toBinary 2 binary$
|
||||
print binary$
|
||||
call toBinary 50 binary$
|
||||
print binary$
|
||||
call toBinary 9000 binary$
|
||||
print binary$
|
||||
9
Task/Binary-digits/EchoLisp/binary-digits.l
Normal file
9
Task/Binary-digits/EchoLisp/binary-digits.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
;; 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
|
||||
27
Task/Binary-digits/Ecstasy/binary-digits.ecstasy
Normal file
27
Task/Binary-digits/Ecstasy/binary-digits.ecstasy
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
module BinaryDigits {
|
||||
@Inject Console console;
|
||||
void run() {
|
||||
Int64[] tests = [0, 1, 5, 50, 9000];
|
||||
|
||||
Int longestInt = tests.map(n -> n.estimateStringLength())
|
||||
.reduce(0, (max, len) -> max.notLessThan(len));
|
||||
Int longestBin = tests.map(n -> (64-n.leadingZeroCount).notLessThan(1))
|
||||
.reduce(0, (max, len) -> max.maxOf(len));
|
||||
|
||||
function String(Int64) num = n -> {
|
||||
Int indent = longestInt - n.estimateStringLength();
|
||||
return $"{' ' * indent}{n}";
|
||||
};
|
||||
|
||||
function String(Int64) 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 (Int64 test : tests) {
|
||||
console.print($"The decimal value {num(test)} should produce an output of {bin(test)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Task/Binary-digits/Elena/binary-digits.elena
Normal file
10
Task/Binary-digits/Elena/binary-digits.elena
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import system'routines;
|
||||
import extensions;
|
||||
|
||||
public program()
|
||||
{
|
||||
new int[]{5,50,9000}.forEach:(n)
|
||||
{
|
||||
console.printLine(n.toString(2))
|
||||
}
|
||||
}
|
||||
1
Task/Binary-digits/Elixir/binary-digits-1.elixir
Normal file
1
Task/Binary-digits/Elixir/binary-digits-1.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
IO.puts Integer.to_string(5, 2)
|
||||
1
Task/Binary-digits/Elixir/binary-digits-2.elixir
Normal file
1
Task/Binary-digits/Elixir/binary-digits-2.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
5 |> Integer.to_string(2) |> IO.puts
|
||||
1
Task/Binary-digits/Elixir/binary-digits-3.elixir
Normal file
1
Task/Binary-digits/Elixir/binary-digits-3.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
[5,50,9000] |> Enum.each(fn n -> IO.puts Integer.to_string(n, 2) end)
|
||||
1
Task/Binary-digits/Elixir/binary-digits-4.elixir
Normal file
1
Task/Binary-digits/Elixir/binary-digits-4.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
Enum.map([5, 50, 9000], fn n -> IO.puts Integer.to_string(n, 2) end)
|
||||
1
Task/Binary-digits/Elixir/binary-digits-5.elixir
Normal file
1
Task/Binary-digits/Elixir/binary-digits-5.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
for n <- [5, 50, 9000] do IO.puts Integer.to_string(n, 2) end
|
||||
10
Task/Binary-digits/Emacs-Lisp/binary-digits.l
Normal file
10
Task/Binary-digits/Emacs-Lisp/binary-digits.l
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(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))
|
||||
16
Task/Binary-digits/Epoxy/binary-digits.epoxy
Normal file
16
Task/Binary-digits/Epoxy/binary-digits.epoxy
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
fn bin(a,b:true)
|
||||
var c:""
|
||||
while a>0 do
|
||||
c,a:tostring(a%2)+c,bit.rshift(a,1)
|
||||
cls
|
||||
if b then
|
||||
c:string.repeat("0",16-#c)+c
|
||||
cls
|
||||
return c
|
||||
cls
|
||||
|
||||
var List: [5,50,9000]
|
||||
|
||||
iter Value of List do
|
||||
log(Value+": "+bin(Value,false))
|
||||
cls
|
||||
1
Task/Binary-digits/Erlang/binary-digits-1.erl
Normal file
1
Task/Binary-digits/Erlang/binary-digits-1.erl
Normal file
|
|
@ -0,0 +1 @@
|
|||
lists:map( fun(N) -> io:fwrite("~.2B~n", [N]) end, [5, 50, 9000]).
|
||||
1
Task/Binary-digits/Erlang/binary-digits-2.erl
Normal file
1
Task/Binary-digits/Erlang/binary-digits-2.erl
Normal file
|
|
@ -0,0 +1 @@
|
|||
[io:fwrite("~.2B~n", [N]) || N <- [5, 50, 9000]].
|
||||
1
Task/Binary-digits/Erlang/binary-digits-3.erl
Normal file
1
Task/Binary-digits/Erlang/binary-digits-3.erl
Normal file
|
|
@ -0,0 +1 @@
|
|||
[io:fwrite("~s~n", [integer_to_list(N, 2)]) || N <- [5, 50, 9000]].
|
||||
13
Task/Binary-digits/Euphoria/binary-digits-1.euphoria
Normal file
13
Task/Binary-digits/Euphoria/binary-digits-1.euphoria
Normal 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')
|
||||
16
Task/Binary-digits/Euphoria/binary-digits-2.euphoria
Normal file
16
Task/Binary-digits/Euphoria/binary-digits-2.euphoria
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
include std/math.e
|
||||
include std/convert.e
|
||||
|
||||
function Bin(integer n, sequence s = "")
|
||||
if n > 0 then
|
||||
return Bin(floor(n/2),(mod(n,2) + #30) & s)
|
||||
end if
|
||||
if length(s) = 0 then
|
||||
return to_integer("0")
|
||||
end if
|
||||
return to_integer(s)
|
||||
end function
|
||||
|
||||
printf(1, "%d\n", Bin(5))
|
||||
printf(1, "%d\n", Bin(50))
|
||||
printf(1, "%d\n", Bin(9000))
|
||||
2
Task/Binary-digits/F-Sharp/binary-digits-1.fs
Normal file
2
Task/Binary-digits/F-Sharp/binary-digits-1.fs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
open System
|
||||
for i in [5; 50; 9000] do printfn "%s" <| Convert.ToString (i, 2)
|
||||
10
Task/Binary-digits/F-Sharp/binary-digits-2.fs
Normal file
10
Task/Binary-digits/F-Sharp/binary-digits-2.fs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
open System
|
||||
|
||||
// define the function
|
||||
let printBin (i: int) =
|
||||
Convert.ToString (i, 2)
|
||||
|> printfn "%s"
|
||||
|
||||
// use the function
|
||||
[5; 50; 9000]
|
||||
|> List.iter printBin
|
||||
10
Task/Binary-digits/F-Sharp/binary-digits-3.fs
Normal file
10
Task/Binary-digits/F-Sharp/binary-digits-3.fs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
open System
|
||||
open System.IO
|
||||
|
||||
// define a callback function for %a
|
||||
let bin (tw: TextWriter) value =
|
||||
tw.Write("{0}", Convert.ToString(int64 value, 2))
|
||||
|
||||
// use it with printfn with %a
|
||||
[5; 50; 9000]
|
||||
|> List.iter (printfn "binary: %a" bin)
|
||||
5
Task/Binary-digits/FALSE/binary-digits.false
Normal file
5
Task/Binary-digits/FALSE/binary-digits.false
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[0\10\[$1&'0+\2/$][]#%[$][,]#%]b:
|
||||
|
||||
5 b;!
|
||||
50 b;!
|
||||
9000 b;!
|
||||
12
Task/Binary-digits/FBSL/binary-digits.fbsl
Normal file
12
Task/Binary-digits/FBSL/binary-digits.fbsl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#AppType Console
|
||||
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 s = "" then return "0"
|
||||
return s
|
||||
end function
|
||||
|
||||
print Bin(5)
|
||||
print Bin(50)
|
||||
print Bin(9000)
|
||||
|
||||
pause
|
||||
15
Task/Binary-digits/FOCAL/binary-digits.focal
Normal file
15
Task/Binary-digits/FOCAL/binary-digits.focal
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
01.10 S A=5;D 2
|
||||
01.20 S A=50;D 2
|
||||
01.30 S A=9000;D 2
|
||||
01.40 Q
|
||||
|
||||
02.10 S BX=0
|
||||
02.20 S BD(BX)=A-FITR(A/2)*2
|
||||
02.25 S A=FITR(A/2)
|
||||
02.30 S BX=BX+1
|
||||
02.35 I (-A)2.2
|
||||
02.40 S BX=BX-1
|
||||
02.45 D 2.6
|
||||
02.50 I (-BX)2.4;T !;R
|
||||
02.60 I (-BD(BX))2.7;T "0";R
|
||||
02.70 T "1"
|
||||
5
Task/Binary-digits/Factor/binary-digits.factor
Normal file
5
Task/Binary-digits/Factor/binary-digits.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
USING: io kernel math math.parser ;
|
||||
|
||||
5 >bin print
|
||||
50 >bin print
|
||||
9000 >bin print
|
||||
18
Task/Binary-digits/Forth/binary-digits.fth
Normal file
18
Task/Binary-digits/Forth/binary-digits.fth
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
\ Forth uses a system variable 'BASE' for number conversion
|
||||
|
||||
\ HEX is a standard word to change the value of base to 16
|
||||
\ DECIMAL is a standard word to change the value of base to 10
|
||||
|
||||
\ we can easily compile a word into the system to set 'BASE' to 2
|
||||
|
||||
: binary 2 base ! ;
|
||||
|
||||
|
||||
\ interactive console test with conversion and binary masking example
|
||||
|
||||
hex 0FF binary . cr
|
||||
decimal 679 binary . cr
|
||||
|
||||
binary 11111111111 00000110000 and . cr
|
||||
|
||||
decimal
|
||||
57
Task/Binary-digits/Fortran/binary-digits.f
Normal file
57
Task/Binary-digits/Fortran/binary-digits.f
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
!-*- mode: compilation; default-directory: "/tmp/" -*-
|
||||
!Compilation started at Sun May 19 23:14:14
|
||||
!
|
||||
!a=./F && make $a && $a < unixdict.txt
|
||||
!f95 -Wall -ffree-form F.F -o F
|
||||
!101
|
||||
!110010
|
||||
!10001100101000
|
||||
!
|
||||
!Compilation finished at Sun May 19 23:14:14
|
||||
!
|
||||
!
|
||||
! tobin=: -.&' '@":@#:
|
||||
! tobin 5
|
||||
!101
|
||||
! tobin 50
|
||||
!110010
|
||||
! tobin 9000
|
||||
!10001100101000
|
||||
|
||||
program bits
|
||||
implicit none
|
||||
integer, dimension(3) :: a
|
||||
integer :: i
|
||||
data a/5,50,9000/
|
||||
do i = 1, 3
|
||||
call s(a(i))
|
||||
enddo
|
||||
|
||||
contains
|
||||
|
||||
subroutine s(a)
|
||||
integer, intent(in) :: a
|
||||
integer :: i
|
||||
if (a .eq. 0) then
|
||||
write(6,'(a)')'0'
|
||||
return
|
||||
endif
|
||||
do i = 31, 0, -1
|
||||
if (btest(a, i)) exit
|
||||
enddo
|
||||
do while (0 .lt. i)
|
||||
if (btest(a, i)) then
|
||||
write(6,'(a)',advance='no')'1'
|
||||
else
|
||||
write(6,'(a)',advance='no')'0'
|
||||
endif
|
||||
i = i-1
|
||||
enddo
|
||||
if (btest(a, i)) then
|
||||
write(6,'(a)')'1'
|
||||
else
|
||||
write(6,'(a)')'0'
|
||||
endif
|
||||
end subroutine s
|
||||
|
||||
end program bits
|
||||
23
Task/Binary-digits/Free-Pascal/binary-digits.pas
Normal file
23
Task/Binary-digits/Free-Pascal/binary-digits.pas
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
program binaryDigits(input, output, stdErr);
|
||||
{$mode ISO}
|
||||
|
||||
function binaryNumber(const value: nativeUInt): shortString;
|
||||
const
|
||||
one = '1';
|
||||
var
|
||||
representation: shortString;
|
||||
begin
|
||||
representation := binStr(value, bitSizeOf(value));
|
||||
// strip leading zeroes, if any; NB: mod has to be ISO compliant
|
||||
delete(representation, 1, (pos(one, representation)-1) mod bitSizeOf(value));
|
||||
// traditional Pascal fashion:
|
||||
// assign result to the (implicitely existent) variable
|
||||
// that is named like the function’s name
|
||||
binaryNumber := representation;
|
||||
end;
|
||||
|
||||
begin
|
||||
writeLn(binaryNumber(5));
|
||||
writeLn(binaryNumber(50));
|
||||
writeLn(binaryNumber(9000));
|
||||
end.
|
||||
9
Task/Binary-digits/FreeBASIC/binary-digits.basic
Normal file
9
Task/Binary-digits/FreeBASIC/binary-digits.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
' 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
|
||||
4
Task/Binary-digits/Frink/binary-digits.frink
Normal file
4
Task/Binary-digits/Frink/binary-digits.frink
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
9000 -> binary
|
||||
9000 -> base2
|
||||
base2[9000]
|
||||
base[9000, 2]
|
||||
2
Task/Binary-digits/FunL/binary-digits.funl
Normal file
2
Task/Binary-digits/FunL/binary-digits.funl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
for n <- [5, 50, 9000, 9000000000]
|
||||
println( n, bin(n) )
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue