langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,27 @@
Function Bitwise
Push $0
Push $1
Push $2
StrCpy $0 7
StrCpy $1 2
IntOp $2 $0 & $1
DetailPrint "Bitwise AND: $0 & $1 = $2"
IntOp $2 $0 | $1
DetailPrint "Bitwise OR: $0 | $1 = $2"
IntOp $2 $0 ^ $1
DetailPrint "Bitwise XOR: $0 ^ $1 = $2"
IntOp $2 $0 ~
DetailPrint "Bitwise NOT (negate in NSIS docs): ~$0 = $2"
DetailPrint "There are no Arithmetic shifts in NSIS"
IntOp $2 $0 >> $1
DetailPrint "Right Shift: $0 >> 1 = $2"
IntOp $2 $0 << $1
DetailPrint "Left Shift: $0 << $1 = $2"
DetailPrint "There are no Rotates in NSIS"
Pop $2
Pop $1
Pop $0
FunctionEnd

View file

@ -0,0 +1,13 @@
def i = 255;
def j = 2;
WriteLine($"$i and $j is $(i & j)");
WriteLine($"$i or $j is $(i | j)");
WriteLine($"$i xor $j is $(i ^ j)");
WriteLine($"not $i is $(~i)");
WriteLine($"$i lshift $j is $(i << j)");
WriteLine($"$i arshift $j is $(i >> j)"); // When the left operand of the >> operator is of a signed integral type,
// the operator performs an arithmetic shift right
WriteLine($"$(i :> uint) rshift $j is $(c >> j)"); // When the left operand of the >> operator is of an unsigned integral type,
// the operator performs a logical shift right
// there are no rotation operators in Nemerle, but you could define your own w/ a macro if you really wanted it

View file

@ -0,0 +1,9 @@
let bitwise a b =
Printf.printf "a and b: %d\n" (a land b);
Printf.printf "a or b: %d\n" (a lor b);
Printf.printf "a xor b: %d\n" (a lxor b);
Printf.printf "not a: %d\n" (lnot a);
Printf.printf "a lsl b: %d\n" (a lsl b); (* left shift *)
Printf.printf "a asr b: %d\n" (a asr b); (* arithmetic right shift *)
Printf.printf "a lsr b: %d\n" (a lsr b); (* logical right shift *)
;;

View file

@ -0,0 +1,17 @@
use IO;
bundle Default {
class Test {
function : Main(args : String[]) ~ Nil {
BitWise(3, 4);
}
function : BitWise(a : Int, b : Int) ~ Nil {
Console->GetInstance()->Print("a and b: ")->PrintLine(a and b);
Console->GetInstance()->Print("a or b: ")->PrintLine(a or b);
Console->GetInstance()->Print("a xor b: ")->PrintLine(a xor b);
# shift left & right are supported by the compiler and VM but not
# exposed to end-users; those instructions are used for optimizations
}
}
}

View file

@ -0,0 +1,11 @@
function bitops(a, b)
s = sprintf("%s %%s %s = %%s\n", dec2bin(a), dec2bin(b));
printf(s, "or", dec2bin(bitor(a, b)));
printf(s, "and", dec2bin(bitand(a, b)));
printf(s, "xor", dec2bin(bitxor(a, b)));
printf(s, "left shift", dec2bin(bitshift(a, abs(b))));
printf(s, "right shift", dec2bin(bitshift(a, -abs(b))));
printf("simul not %s = %s", dec2bin(a), dec2bin(bitxor(a, 0xffffffff)));
endfunction
bitops(0x1e, 0x3);

View file

@ -0,0 +1,8 @@
bo(a,b)={
print("And: "bitand(a,b));
print("Or: "bitor(a,b));
print("Not: "bitneg(a));
print("Xor: "bitxor(a,b));
print("Left shift: ",a<<b);
print("Right shift: ",a>>b);
}

View file

@ -0,0 +1,19 @@
/* PL/I can perform bit operations on binary integers. */
k = iand(i,j);
k = ior(i,j);
k = inot(i,j);
k = ieor(i,j);
k = ishl(i,n); /* unsigned shifts i left by n places. */
k = ishr(i,n); /* unsigned shifts i right by n places. */
k = lower2(i, n); /* arithmetic right shift i by n places. */
k = raise2(i, n); /* arithmetic left shift i by n places. */
/* PL/I can also perform boolean operations on bit strings */
/* of any length: */
declare (s, t, u) bit (*);
u = s & t; /* logical and */
u = s | t; /* logical or */
u = ^ s; /* logical not */
u = s ^ t; /* exclusive or */

View file

@ -0,0 +1,9 @@
var
a, b: integer;
begin
a := 10; { binary 1010 }
b := 12; { binary 1100 }
writeln('a and b = ', a and b); { 8 = 1000 }
writeln('a or b = ', a or b); { 14 = 1110 }
writeln('a xor b = ', a xor b) { 6 = 0110 }
end.

View file

@ -0,0 +1,48 @@
sub bool ($a, $b) {
say 'Coerce to Boolean';
say_bool_buff "$a and $b", $a ?& $b;
say_bool_buff "$a or $b", $a ?| $b;
say_bool_buff "$a xor $b", $a ?^ $b;
say_bool_buff "not $a", !$a;
}
sub buf ($a, $b) {
say 'Coerce to Buffer';
say_bool_buff "$a and $b", $a ~& $b;
say_bool_buff "$a or $b", $a ~| $b;
say_bool_buff "$a xor $b", $a ~^ $b;
# say_bool_buff "$a bit shift right $b", $a ~> $b; #NYI in Rakudo
# say_bool_buff "$a bit shift left $b", $a ~< $b; #NYI in Rakudo
}
sub int ($a, $b) {
say 'Coerce to Int';
say_bit "$a and $b", $a +& $b;
say_bit "$a or $b", $a +| $b;
say_bit "$a xor $b", $a +^ $b;
say_bit "$a signed bit shift right $b", $a +> $b;
# say_bit "$a unsigned bit shift right $b", $a +> $b :unsigned; #NYI in Rakudo
# say_bit "$a rotate right $b", $a +> $b :rotate; #NYI in Rakudo
say_bit "$a bit shift left $b", $a +< $b;
# say_bit "$a rotate shift left $b", $a +< $b :rotate; #NYI in Rakudo
say_bit "twos complement not $a", +^$a;
}
bool(7,2);
say '-' x 80;
buf(7,2);
say '-' x 80;
int(7,2);
say '-' x 80;
sub say_bit ($message, $value) {
my $INTSIZE = $*VM{'config'}{'intvalsize'} * 8; # hack to get native Int size
printf("%30s: %4d, %032b\n", $message, $value, $value) if $INTSIZE == 32;
printf("%30s: %4d, %064b\n", $message, $value, $value) if $INTSIZE == 64;
}
sub say_bool_buff ($message, $value) {
printf("%30s: %4d, %s\n", $message, $value, $value);
}

View file

@ -0,0 +1,30 @@
sub infix:<bsr>( $a, $b, :$rotate, :$unsigned ) {
if $rotate {
my $INTSIZE = $*VM{'config'}{'intvalsize'} * 8; # hack to get native Int size
my $c = $b % $INTSIZE;
return pir::lsr__III($a, $c) +| pir::shl__III((2**$c-1) +& $a, $INTSIZE-$c);
}
if $unsigned {
return pir::lsr__III($a, $b);
}
pir::shr__III($a, $b);
}
sub infix:<bsl>( $a, $b, :$rotate, :$unsigned ) {
if $rotate {
my $INTSIZE = $*VM{'config'}{'intvalsize'} * 8; # hack to get native Int size
my $c = $b % $INTSIZE;
return pir::shl__III($a, $c) +| pir::lsr__III($a, $INTSIZE-$c);
}
pir::shl__III($a, $b);
}
bs_int(7,2);
sub bs_int ($a, $b) {
say_bit "$a Signed Bit shift right $b", $a bsr $b;
say_bit "$a Unsigned Bit shift right $b", infix:<bsr>($a, $b, :unsigned);
say_bit "$a Rotate right $b", infix:<bsr>($a, $b, :rotate);
say_bit "$a Bit shift left $b", $a bsl $b;
say_bit "$a Rotate left $b", infix:<bsl>($a, $b, :rotate);
}

View file

@ -0,0 +1,8 @@
define bitwise(a, b);
printf(a && b, 'a and b = %p\n');
printf(a || b, 'a or b = %p\n');
printf(a ||/& b, 'a xor b = %p\n');
printf(~~ a, 'not a = %p\n');
printf(a << b, 'left shift of a by b = %p\n');
printf(a >> b, 'arithmetic right shift of a by b = %p\n');
enddefine;

View file

@ -0,0 +1,29 @@
Procedure Bitwise(a, b)
Debug a & b ; And
Debug a | b ;Or
Debug a ! b ; XOr
Debug ~a ;Not
Debug a << b ; shift left
Debug a >> b ; arithmetic shift right
; Logical shift right and rotates are not available
; You can of use inline ASM to achieve this:
Define Temp
; logical shift right
!mov edx, dword [p.v_a]
!mov ecx, dword [p.v_b]
!shr edx, cl
!mov dword [p.v_Temp], edx
Debug Temp
; rotate left
!mov edx, dword [p.v_a]
!mov ecx, dword [p.v_b]
!rol edx, cl
!mov dword [p.v_Temp], edx
Debug Temp
; rotate right
!mov edx, dword [p.v_a]
!mov ecx, dword [p.v_b]
!ror edx, cl
!mov dword [p.v_Temp], edx
Debug Temp
EndProcedure

View file

@ -0,0 +1,13 @@
>> x = int(3);
>> y = int(1);
>> z = x && y; printf("0x%08x\n",z); // logical 'and'
0x00000001
>> z = x || y; printf("0x%08x\n",z); // logical 'or'
0x00000003
>> z = !x; printf("0x%08x\n",z); // logical 'not'
0xfffffffc
>> i2 = int(2);
>> z = x * i2; printf("0x%08x\n",z); // left-shift is multiplication by 2 where both arguments are integers
0x00000006
>> z = x / i2; printf("0x%08x\n",z); // right-shift is division by 2 where both arguments are integers
0x00000001

View file

@ -0,0 +1,11 @@
: bitwise ( ab- )
cr
over "a = %d\n" puts
dup "b = %d\n" puts
2over and "a and b = %d\n" puts
2over or "a or b = %d\n" puts
2over xor "a xor b = %d\n" puts
over not "not a = %d\n" puts
2over << "a << b = %d\n" puts
2over >> "a >> b = %d\n" puts
2drop ;

View file

@ -0,0 +1,12 @@
/* rotations are not available, but are easy to implement with the other bitwise operators */
data _null_;
a=105;
b=91;
c=bxor(a,b);
d=band(a,b);
e=bor(a,b);
f=bnot(a); /* on 32 bits */
g=blshift(a,1);
h=brshift(a,1);
put _all_;
run;

View file

@ -0,0 +1,10 @@
[ |:a :b |
inform: (a bitAnd: b) printString.
inform: (a bitOr: b) printString.
inform: (a bitXor: b) printString.
inform: (a bitNot) printString.
inform: (a << b) printString.
inform: (a >> b) printString.
] applyTo: {8. 12}.

View file

@ -0,0 +1,8 @@
fun bitwise_ints (a, b) = (
print ("a and b: " ^ IntInf.toString (IntInf.andb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n");
print ("a or b: " ^ IntInf.toString (IntInf.orb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n");
print ("a xor b: " ^ IntInf.toString (IntInf.xorb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n");
print ("not a: " ^ IntInf.toString (IntInf.notb (IntInf.fromInt a )) ^ "\n");
print ("a lsl b: " ^ IntInf.toString (IntInf.<< (IntInf.fromInt a, Word.fromInt b )) ^ "\n"); (* left shift *)
print ("a asr b: " ^ IntInf.toString (IntInf.~>> (IntInf.fromInt a, Word.fromInt b )) ^ "\n") (* arithmetic right shift *)
)

View file

@ -0,0 +1,9 @@
fun bitwise_words (a, b) = (
print ("a and b: " ^ Word.fmt StringCvt.DEC (Word.andb (a, b)) ^ "\n");
print ("a or b: " ^ Word.fmt StringCvt.DEC (Word.orb (a, b)) ^ "\n");
print ("a xor b: " ^ Word.fmt StringCvt.DEC (Word.xorb (a, b)) ^ "\n");
print ("not a: " ^ Word.fmt StringCvt.DEC (Word.notb a ) ^ "\n");
print ("a lsl b: " ^ Word.fmt StringCvt.DEC (Word.<< (a, b) ) ^ "\n"); (* left shift *)
print ("a asr b: " ^ Word.fmt StringCvt.DEC (Word.~>> (a, b) ) ^ "\n"); (* arithmetic right shift *)
print ("a asr b: " ^ Word.fmt StringCvt.DEC (Word.>> (a, b) ) ^ "\n") (* logical right shift *)
)

View file

@ -0,0 +1,20 @@
program main;
initial begin
bit [52:0] a,b,c;
a = 53'h123476547890fe;
b = 53'h06453bdef23ca6;
c = a & b; $display("%h & %h = %h", a,b,c);
c = a | b; $display("%h | %h = %h", a,b,c);
c = a ^ b; $display("%h ^ %h = %h", a,b,c);
c = ~ a; $display("~%h = %h", a, c);
c = a << 5; $display("%h << 5 = %h", a, c);
c = a >> 5; $display("%h >> 5 = %h", a, c);
c = { a[53-23:0], a[52-:23] }; $display("%h rotate-left 23 = %h", a, c);
c = { a[1:0], a[52:2] }; $display("%h rotate-right 2 = %h", a, c);
end
endprogram

View file

@ -0,0 +1,12 @@
module rotate(in, out, shift);
parameter BITS = 32;
parameter SHIFT_BITS = 5;
input [BITS-1:0] in;
output [BITS-1:0] out;
input [SHIFT_BITS-1:0] shift;
always_comb foreach (out[i]) out[i] = in[ (i+shift) % BITS ];
endmodule

View file

@ -0,0 +1,23 @@
bitwise(a,b)
Prgm
Local show, oldbase
Define show(label, x)=Prgm
Local r
setMode("Base","DEC")
string(x) → r
setMode("Base","HEX")
Disp label & r & " " & string(x)
EndPrgm
getMode("Base") → oldbase
show("", {a, b})
show("And ", a and b)
show("Or ", a or b)
show("Xor ", a xor b)
show("Not ", not a)
Pause "[Press ENTER]"
show("LSh ", shift(a,b))
show("RSh ", shift(a,b))
show("LRo ", rotate(a,b))
show("RRo ", rotate(a,b))
setMode("Base",oldbase)
EndPrgm

View file

@ -0,0 +1,8 @@
Sub Test(a as Integer, b as Integer)
WriteLine("And " & a And b)
WriteLine("Or " & a Or b)
WriteLine("Xor " & a Xor b)
WriteLine("Not " & Not a)
WriteLine("Left Shift " & a << 2)
WriteLine("Right Shift " & a >> 2)
End Sub

View file

@ -0,0 +1,12 @@
Text(0, "A and B = "); HexOut(0, A and B); CrLf(0); \alternate symbol: &
Text(0, "A or B = "); HexOut(0, A or B); CrLf(0); \alternate symbol: !
Text(0, "A xor B = "); HexOut(0, A xor B); CrLf(0); \alternate symbol: |
Text(0, "not A = "); HexOut(0, not A); CrLf(0); \alternate symbol: ~
Text(0, "A << B = "); HexOut(0, A << B); CrLf(0);
Text(0, "A >> B logical = "); HexOut(0, A >> B); CrLf(0);
Text(0, "A >> B arithmetic = "); HexOut(0, A ->> B); CrLf(0);
\Rotate operations must be done by calling a function such as:
func ROR(A, B); int A, B; return A>>B ! A<<(32-B);
Text(0, "A ror B = "); HexOut(0, ROR(A,B)); CrLf(0);