Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View 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

View 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.

View file

@ -0,0 +1 @@
&>0\55+\:2%68>*#<+#8\#62#%/#2:_$>:#,_$@

View 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);

View 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
}

View 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
}

View file

@ -1,11 +1,11 @@
#define system.
#define system'routines.
#define extensions.
// --- Program ---
#symbol program =
[
console writeLine:(convertor toLiteral:5 &base:2).
console writeLine:(convertor toLiteral:50 &base:2).
console writeLine:(convertor toLiteral:9000 &base:2).
(5,50,9000) run &each: n
[
console writeLine:(n toLiteral &base:2).
].
].

View file

@ -0,0 +1 @@
IO.puts Integer.to_string(5,2)

View file

@ -0,0 +1 @@
5 |> Integer.to_string(2) |> IO.puts

View file

@ -0,0 +1 @@
[5,50,9000] |> Enum.map(fn n -> IO.puts Integer.to_string(n,2) end)

View 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))

View 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

View file

@ -1,4 +1,18 @@
9000 50 5
2 base !
. . .
decimal
\ 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 ! ; ok
\ interactive console test with conversion and binary masking example
hex 0FF binary . 11111111 ok
decimal 679 binary . 1010100111 ok
ok
binary 11111111111 00000110000 and . 110000 ok
decimal ok

View file

@ -0,0 +1,4 @@
9000 -> binary
9000 -> base2
base2[9000]
base[9000. 2]

View file

@ -0,0 +1,7 @@
console.log(
[5, 50, 9000].map(function (n) {
return (n).toString(2);
}).join('\n')
)

View file

@ -0,0 +1,3 @@
for i in [0, 5, 50, 9000]
println(i, " => ", bin(i))
end

View file

@ -0,0 +1,13 @@
function dec2bin (n)
local bin, number, bit = "", tonumber(n) -- can pass n as string
while number > 0 do
bit = number % 2
number = math.floor(number/2)
bin = bit .. bin
end
return bin
end
print(dec2bin(5))
print(dec2bin(50))
print(dec2bin(9000))

View file

@ -1,5 +1,5 @@
fn main() {
for i in range(0, 16) {
println!("{:t}", i)
for i in 0..8 {
println!("{:b}", i)
}
}

View file

@ -0,0 +1,5 @@
/recurse\
$,binary!\@\>?!\@/<@\.#
! \=/ \=itoa=@@@+@+++++#
/<+>- \ div2
\?!#-?/+# mod2

View file

@ -0,0 +1,12 @@
:Input "DEC: ",D
:" →Str1
:If not(D:"0→Str1
:While D>0
:If not(fPart(D/2:Then
:"0"+Str1→Str1
:Else
:"1"+Str1→Str1
:End
:iPart(D/2→D
:End
:Disp Str1