Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,52 @@
; bitwise AND
anl a, b
; bitwise OR
orl a, b
; bitwise XOR
xrl a, b
; bitwise NOT
cpl a
; left shift
inc b
rrc a
loop:
rlc a
clr c
djnz b, loop
; right shift
inc b
rlc a
loop:
rrc a
clr c
djnz b, loop
; arithmetic right shift
push 20
inc b
rlc a
mov 20.0, c
loop:
rrc a
mov c, 20.0
djnz b, loop
pop 20
; left rotate
inc b
rr a
loop:
rl a
djnz b, loop
; right rotate
inc b
rl a
loop:
rr a
djnz b, loop

View file

@ -0,0 +1,9 @@
Lbl BITS
r₁→A
r₂→B
Disp "AND:",A·B▶Dec,i
Disp "OR:",AᕀB▶Dec,i
Disp "XOR:",A▫B▶Dec,i
Disp "NOT:",not(A)ʳ▶Dec,i
.No language support for shifts or rotations
Return

View file

@ -0,0 +1,29 @@
BitwiseOperations(INTEGER A, INTEGER B) := FUNCTION
BitAND := A & B;
BitOR := A | B;
BitXOR := A ^ B;
BitNOT := BNOT A;
BitSL := A << B;
BitSR := A >> B;
DS := DATASET([{A,B,'Bitwise AND:',BitAND},
{A,B,'Bitwise OR:',BitOR},
{A,B,'Bitwise XOR',BitXOR},
{A,B,'Bitwise NOT A:',BitNOT},
{A,B,'ShiftLeft A:',BitSL},
{A,B,'ShiftRight A:',BitSR}],
{INTEGER AVal,INTEGER BVal,STRING15 valuetype,INTEGER val});
RETURN DS;
END;
BitwiseOperations(255,5);
//right arithmetic shift, left and right rotate not implemented
/*
OUTPUT:
255 5 Bitwise AND: 5
255 5 Bitwise OR: 255
255 5 Bitwise XOR 250
255 5 Bitwise NOT A: -256
255 5 ShiftLeft A: 8160
255 5 ShiftRight A: 7
*/

View file

@ -0,0 +1,70 @@
' FB 1.05.0 Win64 (Note the (U)Integer type is 64 bits)
' FB doesn't have built-in logical shift right or rotation operators
' but, as they're not difficult to implement, I've done so below.
Function lsr(x As Const Integer, y As Const Integer) As Integer
Dim As UInteger z = x
Return z Shr y
End Function
Function rol(x As Const Integer, y As Const UInteger) As Integer
Dim z As Integer = x
Dim high As Integer
For i As Integer = 1 To y
high = Bit(z, 63)
For j As Integer = 62 To 0 Step -1
If Bit(z, j) Then
z = BitSet(z, j + 1)
Else
z = BitReset (z, j + 1)
End If
Next j
If high Then
z = BitSet(z, 0)
Else
z = BitReset(z, 0)
End If
Next i
Return z
End Function
Function ror(x As Const Integer, y As Const UInteger) As Integer
Dim z As Integer = x
Dim low As Integer
For i As Integer = 1 To y
low = Bit(z, 0)
For j As Integer = 1 To 63
If Bit(z, j) Then
z = BitSet(z, j - 1)
Else
z = BitReset (z, j - 1)
End If
Next j
If low Then
z = BitSet(z, 63)
Else
z = BitReset(z, 63)
End If
Next i
Return z
End Function
Sub bitwise(x As Integer, y As Integer)
Print "x = "; x
Print "y = "; y
Print "x AND y = "; x And y
Print "x OR y = "; x Or y
Print "x XOR y = "; x XOr y
Print "NOT x = "; Not x
Print "x SHL y = "; x Shl y
Print "x SHR y = "; x Shr y
Print "x LSR y = "; lsr(x, y)
Print "x ROL y = "; rol(x, y)
Print "x ROR y = "; ror(x, y)
End Sub
bitwise -15, 3
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,29 @@
include "ConsoleWindow"
// Set tab width for printing
def tab 1
local fn rotl( b as long, n as long ) as long
end fn = ( ( 2^n * b) mod 256) or (b > 127)
local fn rotr( b as long, n as long ) as long
end fn = (b >> n mod 32) or ( b << (32-n) mod 32)
local fn bitwise( a as long, b as long )
print "Input: a = "; a; " b = "; b
print
print "AND :", "a && b = ", bin$(a && b), ": "; a && b
print "NAND :", "a ^& b = ", bin$(a ^& b), ": "; a ^& b
print "OR :", "a || b = ", bin$(a || b), ": "; a || b
print "NOR :", "a ^| b = ", bin$(a ^| b), ": "; a ^| b
print "XOR :", "a ^^ b = ", bin$(a ^^ b), ": "; a ^^ b
print "NOT :", " not a = ", bin$( not a), ": "; not a
print
print "Left shift :", "a << b =", bin$(a << b), ": "; a << b
print "Right shift :", "a >> b =", bin$(a >> b), ": "; a >> b
print
print "Rotate left :", "fn rotl( a, b ) = ", bin$(fn rotl( a, b)), ": "; fn rotl( a, b )
print "Rotate right :", "fn rotr( a, b ) = ", bin$(fn rotr( a, b )),": "; fn rotr( a, b )
end fn
fn bitwise( 255, 2 )

View file

@ -0,0 +1,10 @@
EXPORT BITOPS(a, b)
BEGIN
PRINT(BITAND(a, b));
PRINT(BITOR(a, b));
PRINT(BITXOR(a, b));
PRINT(BITNOT(a));
PRINT(BITSL(a, b));
PRINT(BITSR(a, b));
// HPPPL has no builtin rotates or arithmetic right shift.
END;

View file

@ -0,0 +1,25 @@
(defun bitwise (a b)
(io:format '"~p~n" (list (band a b)))
(io:format '"~p~n" (list (bor a b)))
(io:format '"~p~n" (list (bxor a b)))
(io:format '"~p~n" (list (bnot a)))
(io:format '"~p~n" (list (bsl a b)))
(io:format '"~p~n" (list (bsr a b))))
(defun d2b
(x) (integer_to_list x 2))
(defun bitwise
((a b 'binary)
(io:format '"(~s ~s ~s): ~s~n"
(list "band" (d2b a) (d2b b) (d2b (band a b))))
(io:format '"(~s ~s ~s): ~s~n"
(list "bor" (d2b a) (d2b b) (d2b (bor a b))))
(io:format '"(~s ~s ~s): ~s~n"
(list "bxor" (d2b a) (d2b b) (d2b (bxor a b))))
(io:format '"(~s ~s): ~s~n"
(list "bnot" (d2b a) (d2b (bnot a))))
(io:format '"(~s ~s ~s): ~s~n"
(list "bsl" (d2b a) (d2b b) (d2b (bsl a b))))
(io:format '"(~s ~s ~s): ~s~n"
(list "bsr" (d2b a) (d2b b) (d2b (bsr a b))))))

View file

@ -0,0 +1,17 @@
> (bitwise 255 170)
170
255
85
-256
381627307539845370001346183518875822092557105621893120
0
ok
> (bitwise 255 170 'binary)
(band 11111111 10101010): 10101010
(bor 11111111 10101010): 11111111
(bxor 11111111 10101010): 1010101
(bnot 11111111): -100000000
(bsl 11111111 10101010): 1111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
(bsr 11111111 10101010): 0
ok
>

View file

@ -0,0 +1,4 @@
put bitAND(2,7)
put bitOR(2,7)
put bitXOR(2,7)
put bitNOT(7)

View file

@ -0,0 +1,8 @@
put "and:" && (255 bitand 2) & comma into bitops
put " or:" && (255 bitor 2) & comma after bitops
put " xor:" && (255 bitxor 2) & comma after bitops
put " not:" && (bitnot 255) after bitops
put bitops
-- Ouput
and: 2, or: 255, xor: 253, not: 4294967040

View file

@ -0,0 +1,7 @@
proc bitwise(a, b) =
echo "a and b: " , a and b
echo "a or b: ", a or b
echo "a xor b: ", a xor b
echo "not a: ", not a
echo "a << b: ", a shl b
echo "a >> b: ", a shr b

View file

@ -0,0 +1,6 @@
: bitwise(a, b)
a b bitAnd println
a b bitOr println
a b bitXor println
a bitLeft(b) println
a bitRight(b) println ;

View file

@ -0,0 +1,88 @@
enum SHL, SAR, SHR, ROL, ROR
function bitop(atom a, integer b, integer op)
atom res
#ilASM{
[32]
mov eax,[a]
call :%pLoadMint
mov ecx,[b]
mov edx,[op]
cmp dl,SHL
jne @f
shl eax,cl
jmp :storeres
@@:
cmp dl,SAR
jne @f
sar eax,cl
jmp :storeres
@@:
cmp dl,SHR
jne @f
shr eax,cl
jmp :storeres
@@:
cmp dl,ROL
jne @f
rol eax,cl
jmp :storeres
@@:
cmp dl,ROR
jne @f
ror eax,cl
jmp :storeres
@@:
int3
::storeres
lea edi,[res]
call :%pStoreMint
[64]
mov rax,[a]
mov rcx,[b]
mov edx,[op]
cmp dl,SHL
jne @f
shl rax,cl
jmp :storeres
@@:
cmp dl,SAR
jne @f
sar rax,cl
jmp :storeres
@@:
cmp dl,SHR
jne @f
shr rax,cl
jmp :storeres
@@:
cmp dl,ROL
jne @f
rol rax,cl
jmp :storeres
@@:
cmp dl,ROR
jne @f
ror eax,cl
jmp :storeres
@@:
int3
::storeres
lea rdi,[res]
call :%pStoreMint
}
return res
end function
procedure bitwise(atom a, atom b)
printf(1,"and_bits(%b,%b) = %032b\n",{a,b,and_bits(a,b)})
printf(1," or_bits(%b,%b) = %032b\n",{a,b, or_bits(a,b)})
printf(1,"xor_bits(%b,%b) = %032b\n",{a,b,xor_bits(a,b)})
printf(1,"not_bits(%b) = %032b\n",{a,not_bits(a)})
printf(1," shl(%b,%b) = %032b\n",{a,b,bitop(a,b,SHL)})
printf(1," sar(%b,%b) = %032b\n",{a,b,bitop(a,b,SAR)})
printf(1," shr(%b,%b) = %032b\n",{a,b,bitop(a,b,SHR)})
printf(1," rol(%b,%b) = %032b\n",{a,b,bitop(a,b,ROL)})
printf(1," ror(%b,%b) = %032b\n",{a,b,bitop(a,b,ROR)})
end procedure
bitwise(0x800000FE,7)

View file

@ -0,0 +1,9 @@
x = 8
y = 2
see "x & y - Binary AND : " + (x & y) + nl
see "x | y - Binary OR : " + (x | y) + nl
see "x ^ y - Binary XOR : " + (x ^ y) +nl
see "~x - Binary Ones Complement : " + (~x) + nl
see "x << y - Binary Left Shift : " + (x << y) + nl
see "x >> y - Binary Right Shift : " + (x >> y) + nl

View file

@ -0,0 +1,15 @@
func bitwise(a, b) {
# Make sure they are integers
a.to_int!;
b.to_int!;
say ('a and b : ', a & b);
say ('a or b : ', a | b);
say ('a xor b : ', a ^ b);
say ('not a : ', ~a);
say ('a << b : ', a << b); # left shift
say ('a >> b : ', a >> b); # arithmetic right shift
}
bitwise(14,3)

View file

@ -0,0 +1,15 @@
func bitwise(a: Int, b: Int) {
// All bitwise operations (including shifts)
// require both operands to be the same type
println("a AND b: \(a & b)")
println("a OR b: \(a | b)")
println("a XOR b: \(a ^ b)")
println("NOT a: \(~a)")
println("a << b: \(a << b)") // left shift
// for right shifts, if the operands are unsigned, Swift performs
// a logical shift; if signed, an arithmetic shift.
println("a >> b: \(a >> b)") // arithmetic right shift
println("a lsr b: \(Int(bitPattern: UInt(bitPattern: a) >> UInt(bitPattern: b)))") // logical right shift
}
bitwise(-15,3)