Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -4,5 +4,4 @@ o_xinteger(2, 5);
|
|||
o_byte('\n');
|
||||
o_xinteger(2, 50);
|
||||
o_byte('\n');
|
||||
o_xinteger(2, 9000);
|
||||
o_byte('\n');
|
||||
o_form("/x2/\n", 9000);
|
||||
|
|
|
|||
|
|
@ -1,25 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <bitset>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#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';
|
||||
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);
|
||||
int main() {
|
||||
print_bin(0);
|
||||
print_bin(5);
|
||||
print_bin(50);
|
||||
print_bin(9000);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
foreach (i; 0 .. 16)
|
||||
import std.stdio;
|
||||
|
||||
foreach (immutable i; 0 .. 16)
|
||||
writefln("%b", i);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,37 @@
|
|||
function IntToBinStr(AInt : integer) : string;
|
||||
{$IFDEF FPC}
|
||||
{$MODE DELPHI}
|
||||
{$OPTIMIZATION ON,Regvar,ASMCSE,CSE,PEEPHOLE}
|
||||
{$ELSE}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
uses
|
||||
sysutils; //only for timing
|
||||
|
||||
function IntToBinStrNew(AInt : LongWord):string;
|
||||
const
|
||||
IO : array[0..1] of char = ('.','X');
|
||||
var
|
||||
idx,m : LongWord;
|
||||
pC : pChar;
|
||||
begin
|
||||
IF AInt = 0 then Begin
|
||||
result := IO[0];EXIT;end;
|
||||
// search for the first set bit
|
||||
idx:= 32;m := 1 shl (idx-1);
|
||||
While ORD((AInt AND m) = 0)+ORD(m>0) = 2 do begin
|
||||
dec(idx);m := m shr 1;end;
|
||||
|
||||
//set right length and insert one by one
|
||||
setlength(result,idx);
|
||||
pC := @result[1];
|
||||
repeat
|
||||
pC^ := IO[ORD((AInt and m) <> 0)];
|
||||
m := m shr 1;
|
||||
inc(pC);
|
||||
until m=0;
|
||||
end;
|
||||
|
||||
function IntToBinStr(AInt : LongWord) : string;
|
||||
begin
|
||||
Result := '';
|
||||
repeat
|
||||
|
|
@ -9,7 +42,24 @@ end;
|
|||
|
||||
procedure Binary_Digits;
|
||||
begin
|
||||
writeln(' 5: '+IntToBinStr(5));
|
||||
writeln(' 50: '+IntToBinStr(50));
|
||||
writeln(' 5: ',IntToBinStr(5));
|
||||
writeln(' 5: ',IntToBinStrNew(5));
|
||||
writeln(' 50: ',IntToBinStr(50));
|
||||
writeln(' 50: ',IntToBinStrNew(50));
|
||||
writeln('9000: '+IntToBinStr(9000));
|
||||
writeln('9000: '+IntToBinStrNew(9000));
|
||||
end;
|
||||
|
||||
var
|
||||
i: LongInt;
|
||||
t :TDateTime;
|
||||
Begin
|
||||
Binary_Digits;
|
||||
//speed test
|
||||
t := time;
|
||||
For i := 1 to 10*1000*1000 do
|
||||
IntToBinStrNew(i);t := time-t; Writeln(' New ',t*86400.0:6:3);
|
||||
t := time;
|
||||
For i := 1 to 10*1000*1000 do
|
||||
IntToBinStr(i);t := time-t; Writeln(' Old ',t*86400.0:6:3);
|
||||
end.
|
||||
|
|
|
|||
85
Task/Binary-digits/Oberon-2/binary-digits.oberon-2
Normal file
85
Task/Binary-digits/Oberon-2/binary-digits.oberon-2
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
MODULE BinaryDigits;
|
||||
IMPORT
|
||||
Object,
|
||||
SYSTEM,
|
||||
Out;
|
||||
|
||||
PROCEDURE Reverse(VAR str: ARRAY OF CHAR);
|
||||
VAR
|
||||
s,e: LONGINT;
|
||||
c: CHAR;
|
||||
BEGIN
|
||||
e := LEN(str) - 1;
|
||||
WHILE (e >= 0) & (str[e] = 0X) DO DEC(e) END;
|
||||
s := 0;
|
||||
WHILE (s < e) DO
|
||||
c := str[s];
|
||||
str[s] := str[e];
|
||||
str[e] := c;
|
||||
INC(s);DEC(e)
|
||||
END
|
||||
END Reverse;
|
||||
|
||||
|
||||
PROCEDURE IntToOct*(x: LONGINT):STRING;
|
||||
VAR
|
||||
i: LONGINT;
|
||||
o: ARRAY 12 OF CHAR;
|
||||
BEGIN
|
||||
o[LEN(o) - 1] := 0X;
|
||||
|
||||
i := 0;
|
||||
WHILE (i < LEN(o) - 1) DO
|
||||
o[i] := CHR(ORD('0') + (x MOD 8));
|
||||
INC(i);x := SYSTEM.LSH(x,-3)
|
||||
END;
|
||||
Reverse(o);
|
||||
RETURN Object.NewLatin1(o)
|
||||
END IntToOct;
|
||||
|
||||
PROCEDURE IntToHex*(x: LONGINT):STRING;
|
||||
VAR
|
||||
i: LONGINT;
|
||||
h: ARRAY 9 OF CHAR;
|
||||
hexDigit: LONGINT;
|
||||
BEGIN
|
||||
h[LEN(h) - 1] := 0X;
|
||||
|
||||
i := 0;
|
||||
WHILE (i < LEN(h) - 1) DO
|
||||
hexDigit := x MOD 16;
|
||||
IF (hexDigit >= 0) & (hexDigit <= 9) THEN
|
||||
h[i] := CHR(ORD('0') + hexDigit);
|
||||
ELSE
|
||||
h[i] := CHR(ORD('A') + (hexDigit - 10));
|
||||
END;
|
||||
INC(i);x := SYSTEM.LSH(x,-4)
|
||||
END;
|
||||
Reverse(h);
|
||||
RETURN Object.NewLatin1(h)
|
||||
END IntToHex;
|
||||
|
||||
PROCEDURE IntToBin*(x: LONGINT):STRING;
|
||||
VAR
|
||||
i: LONGINT;
|
||||
b: ARRAY 33 OF CHAR;
|
||||
BEGIN
|
||||
b[LEN(b) - 1] := 0X;
|
||||
i := 0;
|
||||
WHILE (i < LEN(b) - 1) DO
|
||||
b[i] := CHR(ORD('0') + (x MOD 2));
|
||||
INC(i);x := SYSTEM.LSH(x,-1)
|
||||
END;
|
||||
|
||||
Reverse(b);
|
||||
RETURN Object.NewLatin1(b);
|
||||
END IntToBin;
|
||||
|
||||
BEGIN
|
||||
|
||||
Out.Object("12 :> " + IntToBin(12));Out.Ln;
|
||||
Out.Object("-12 :> " + IntToBin(-12));Out.Ln;
|
||||
Out.Object("MAX(LONGINT) :> " + IntToBin(MAX(LONGINT)));Out.Ln;
|
||||
Out.Object("MIN(LONGINT) :> " + IntToBin(MIN(LONGINT)));Out.Ln;
|
||||
|
||||
END BinaryDigits.
|
||||
5
Task/Binary-digits/Rust/binary-digits.rust
Normal file
5
Task/Binary-digits/Rust/binary-digits.rust
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
for i in range(0, 16) {
|
||||
println!("{:t}", i)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
import scala.language.postfixOps
|
||||
object BinaryDigits {
|
||||
(5 toBinaryString).reverse.padTo(14, '0').reverse
|
||||
//> res0: String = 00000000000101
|
||||
(50 toBinaryString).reverse.padTo(14, '0').reverse
|
||||
//> res1: String = 00000000110010
|
||||
(9000 toBinaryString).reverse.padTo(14, '0').reverse
|
||||
//> res2: String = 10001100101000
|
||||
}
|
||||
scala> (5 toBinaryString)
|
||||
res0: String = 101
|
||||
|
||||
scala> (50 toBinaryString)
|
||||
res1: String = 110010
|
||||
|
||||
scala> (9000 toBinaryString)
|
||||
res2: String = 10001100101000
|
||||
|
|
|
|||
|
|
@ -5,6 +5,6 @@ const proc: main is func
|
|||
var integer: number is 0;
|
||||
begin
|
||||
for number range 0 to 16 do
|
||||
writeln(str(number, 2));
|
||||
writeln(number radix 2);
|
||||
end for;
|
||||
end func;
|
||||
|
|
|
|||
21
Task/Binary-digits/Vim-Script/binary-digits.vim
Normal file
21
Task/Binary-digits/Vim-Script/binary-digits.vim
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
function Num2Bin(n)
|
||||
let n = a:n
|
||||
let s = ""
|
||||
if n == 0
|
||||
let s = "0"
|
||||
else
|
||||
while n
|
||||
if n % 2 == 0
|
||||
let s = "0" . s
|
||||
else
|
||||
let s = "1" . s
|
||||
endif
|
||||
let n = n / 2
|
||||
endwhile
|
||||
endif
|
||||
return s
|
||||
endfunction
|
||||
|
||||
echo Num2Bin(5)
|
||||
echo Num2Bin(50)
|
||||
echo Num2Bin(9000)
|
||||
Loading…
Add table
Add a link
Reference in a new issue