2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1 +1,9 @@
{{basic data operation}}Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate. All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer. If any operation is not available in your language, note it.
{{basic data operation}}
;Task:
Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate.
All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer.
If any operation is not available in your language, note it.
<br><br>

View file

@ -1,24 +1,23 @@
with Ada.Text_Io; use Ada.Text_Io;
with Interfaces; use Interfaces;
with Ada.Text_IO, Interfaces;
use Ada.Text_IO, Interfaces;
procedure Bitwise is
subtype Byte is Unsigned_8;
package Byte_Io is new Ada.Text_Io.Modular_Io(Byte);
procedure Bitwise is
subtype Byte is Unsigned_8;
package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);
A : Byte := 255;
B : Byte := 170;
X : Byte := 128;
N : Natural := 1;
begin
Put_Line("A and B = "); Byte_Io.Put(Item => A and B, Base => 2);
Put_Line("A or B = "); Byte_IO.Put(Item => A or B, Base => 2);
Put_Line("A xor B = "); Byte_Io.Put(Item => A xor B, Base => 2);
Put_Line("Not A = "); Byte_IO.Put(Item => not A, Base => 2);
New_Line(2);
Put_Line(Unsigned_8'Image(Shift_Left(X, N))); -- Left shift
Put_Line(Unsigned_8'Image(Shift_Right(X, N))); -- Right shift
Put_Line(Unsigned_8'Image(Shift_Right_Arithmetic(X, N))); -- Right Shift Arithmetic
Put_Line(Unsigned_8'Image(Rotate_Left(X, N))); -- Left rotate
Put_Line(Unsigned_8'Image(Rotate_Right(X, N))); -- Right rotate
end bitwise;
A : constant Byte := 2#00011110#;
B : constant Byte := 2#11110100#;
X : constant Byte := 128;
N : constant Natural := 1;
begin
Put ("A and B = "); Byte_IO.Put (Item => A and B, Base => 2); New_Line;
Put ("A or B = "); Byte_IO.Put (Item => A or B, Base => 2); New_Line;
Put ("A xor B = "); Byte_IO.Put (Item => A xor B, Base => 2); New_Line;
Put ("not A = "); Byte_IO.Put (Item => not A, Base => 2); New_Line;
New_Line (2);
Put_Line (Unsigned_8'Image (Shift_Left (X, N)));
Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
Put_Line (Unsigned_8'Image (Rotate_Left (X, N)));
Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;

View file

@ -2,7 +2,7 @@
a = 0b00010001
b = 0b11110000
print a
print int(a * 2) # right shift (multiply by 2)
print a \ 2 # left shift (integer divide by 2)
print int(a * 2) # shift left (multiply by 2)
print a \ 2 # shift right (integer divide by 2)
print a | b # bitwise or on two integer values
print a & b # bitwise or on two integer values

View file

@ -1,10 +1 @@
((main { (5 9) foo ! })
(foo {
({cand} {cor} {cnor} {cxor} {cxnor} {cushl} {cushr} {cashr} {curol} {curor})
{ <- dup give ->
eval
%x nl <<}
each
give zap
cnot %x nl <<}))
({5 9}) ({cand} {cor} {cnor} {cxor} {cxnor} {shl} {shr} {ashr} {rol}) cart ! {give <- cp -> compose !} over ! {eval} over ! {;} each

View file

@ -1,11 +1 @@
1
d
fffffff7
c
fffffff3
a00
0
0
a00
2800000
fffffffa
9 cnot ;

View file

@ -0,0 +1,9 @@
(defun shl (x width bits)
"Compute bitwise left shift of x by 'bits' bits, represented on 'width' bits"
(logand (ash x bits)
(1- (ash 1 width))))
(defun shr (x width bits)
"Compute bitwise right shift of x by 'bits' bits, represented on 'width' bits"
(logand (ash x (- bits))
(1- (ash 1 width))))

View file

@ -0,0 +1,13 @@
(defun rotl (x width bits)
"Compute bitwise left rotation of x by 'bits' bits, represented on 'width' bits"
(logior (logand (ash x (mod bits width))
(1- (ash 1 width)))
(logand (ash x (- (- width (mod bits width))))
(1- (ash 1 width)))))
(defun rotr (x width bits)
"Compute bitwise right rotation of x by 'bits' bits, represented on 'width' bits"
(logior (logand (ash x (- (mod bits width)))
(1- (ash 1 width)))
(logand (ash x (- width (mod bits width)))
(1- (ash 1 width)))))

View file

@ -1,5 +1,5 @@
#define system.
#define extensions.
#import system.
#import extensions.
#class(extension) testOp
{
@ -8,7 +8,7 @@
console writeLine:self:" and ":y:" = ":(self and:y).
console writeLine:self:" or ":y:" = ":(self or:y).
console writeLine:self:" xor ":y:" = ":(self xor:y).
console writeLine:"not ":self:" = ":(self not).
console writeLine:"not ":self:" = ":(self inverted).
console writeLine:self:" shr ":y:" = ":(self shift &index:y).
console writeLine:self:" shl ":y:" = ":(self shift &index:(y negative)).
]

View file

@ -15,8 +15,8 @@ write(*,fmt1) 'input a=',a,' b=',b
write(*,fmt2) 'and : ', a,' & ',b,' = ',iand(a, b),iand(a, b)
write(*,fmt2) 'or : ', a,' | ',b,' = ',ior(a, b),ior(a, b)
write(*,fmt2) 'xor : ', a,' ^ ',b,' = ',ieor(a, b),ieor(a, b)
write(*,fmt2) 'lsh : ', a,' << ',b,' = ',ishft(a, abs(b)),ishft(a, abs(b))
write(*,fmt2) 'rsh : ', a,' >> ',b,' = ',ishft(a, -abs(b)),ishft(a, -abs(b))
write(*,fmt2) 'lsh : ', a,' << ',b,' = ',shiftl(a,b),shiftl(a,b) !since F2008, otherwise use ishft(a, abs(b))
write(*,fmt2) 'rsh : ', a,' >> ',b,' = ',shiftr(a,b),shiftr(a,b) !since F2008, otherwise use ishft(a, -abs(b))
write(*,fmt2) 'not : ', a,' ~ ',b,' = ',not(a),not(a)
write(*,fmt2) 'rot : ', a,' r ',b,' = ',ishftc(a,-abs(b)),ishftc(a,-abs(b))

View file

@ -2,44 +2,37 @@ package main
import "fmt"
func bitwise(a, b int16) (and, or, xor, not, shl, shr, ras, rol, ror int16) {
// the first four are easy
and = a & b
or = a | b
xor = a ^ b
not = ^a
func bitwise(a, b int16) {
fmt.Printf("a: %016b\n", uint16(a))
fmt.Printf("b: %016b\n", uint16(b))
// for all shifts, the right operand (shift distance) must be unsigned.
// use abs(b) for a non-negative value.
if b < 0 {
b = -b
}
ub := uint(b)
// Bitwise logical operations
fmt.Printf("and: %016b\n", uint16(a&b))
fmt.Printf("or: %016b\n", uint16(a|b))
fmt.Printf("xor: %016b\n", uint16(a^b))
fmt.Printf("not: %016b\n", uint16(^a))
shl = a << ub
// for right shifts, if the left operand is unsigned, Go performs
// a logical shift; if signed, an arithmetic shift.
shr = int16(uint16(a) >> ub)
ras = a >> ub
if b < 0 {
fmt.Println("Right operand is negative, but all shifts require an unsigned right operand (shift distance).")
return
}
ua := uint16(a)
ub := uint32(b)
// rotates
rol = a << ub | int16(uint16(a) >> (16-ub))
ror = int16(uint16(a) >> ub) | a << (16-ub)
return
// Logical shifts (unsigned left operand)
fmt.Printf("shl: %016b\n", uint16(ua<<ub))
fmt.Printf("shr: %016b\n", uint16(ua>>ub))
// Arithmetic shifts (signed left operand)
fmt.Printf("las: %016b\n", uint16(a<<ub))
fmt.Printf("ras: %016b\n", uint16(a>>ub))
// Rotations
fmt.Printf("rol: %016b\n", uint16(a<<ub|int16(uint16(a)>>(16-ub))))
fmt.Printf("ror: %016b\n", uint16(int16(uint16(a)>>ub)|a<<(16-ub)))
}
func main() {
var a, b int16 = -460, 6
and, or, xor, not, shl, shr, ras, rol, ror := bitwise(a, b)
fmt.Printf("a: %016b\n", uint16(a))
fmt.Printf("b: %016b\n", uint16(b))
fmt.Printf("and: %016b\n", uint16(and))
fmt.Printf("or: %016b\n", uint16(or))
fmt.Printf("xor: %016b\n", uint16(xor))
fmt.Printf("not: %016b\n", uint16(not))
fmt.Printf("shl: %016b\n", uint16(shl))
fmt.Printf("shr: %016b\n", uint16(shr))
fmt.Printf("ras: %016b\n", uint16(ras))
fmt.Printf("rol: %016b\n", uint16(rol))
fmt.Printf("ror: %016b\n", uint16(ror))
var a, b int16 = -460, 6
bitwise(a, b)
}

View file

@ -0,0 +1,26 @@
MODULE Bitwise;
IMPORT
SYSTEM,
Out;
PROCEDURE Do(a,b: LONGINT);
VAR
x,y: SET;
BEGIN
x := SYSTEM.VAL(SET,a);y := SYSTEM.VAL(SET,b);
Out.String("a and b :> ");Out.Int(SYSTEM.VAL(LONGINT,x * y),0);Out.Ln;
Out.String("a or b :> ");Out.Int(SYSTEM.VAL(LONGINT,x + y),0);Out.Ln;
Out.String("a xor b :> ");Out.Int(SYSTEM.VAL(LONGINT,x / y),0);Out.Ln;
Out.String("a and ~b:> ");Out.Int(SYSTEM.VAL(LONGINT,x - y),0);Out.Ln;
Out.String("~a :> ");Out.Int(SYSTEM.VAL(LONGINT,-x),0);Out.Ln;
Out.String("a left shift b :> ");Out.Int(SYSTEM.VAL(LONGINT,SYSTEM.LSH(x,b)),0);Out.Ln;
Out.String("a right shift b :> ");Out.Int(SYSTEM.VAL(LONGINT,SYSTEM.LSH(x,-b)),0);Out.Ln;
Out.String("a left rotate b :> ");Out.Int(SYSTEM.VAL(LONGINT,SYSTEM.ROT(x,b)),0);Out.Ln;
Out.String("a right rotate b :> ");Out.Int(SYSTEM.VAL(LONGINT,SYSTEM.ROT(x,-b)),0);Out.Ln;
Out.String("a arithmetic left shift b :> ");Out.Int(SYSTEM.VAL(LONGINT,ASH(a,b)),0);Out.Ln;
Out.String("a arithmetic right shift b :> ");Out.Int(SYSTEM.VAL(LONGINT,ASH(a,-b)),0);Out.Ln
END Do;
BEGIN
Do(10,2);
END Bitwise.

View file

@ -1,34 +1,27 @@
/*REXX program performs bitwise operations on integers: & | && ¬ «L »R */
numeric digits 1000 /*be able to handle big integers.*/
/*REXX program performs bitwise operations on integers: & | && ¬ «L »R */
numeric digits 1000 /*be able to handle some big integers. */
say center('decimal', 9) center("value", 9) center('bits', 50)
say copies('' , 9) copies("" , 9) copies('', 50)
say center('decimal',9) center("value",9) center('bits',50)
say copies('',9) copies('',9) copies('',50)
a = 21 ; call show a , 'A' /* show & tell A */
b = 3 ; call show b , 'B' /* show & tell B */
a = 21 ; call show a , 'A' /* show & tell A */
b = 3 ; call show b , 'B' /* show & tell B */
call show bAnd(a,b) , 'A & B' /* and */
call show bOr( a,b) , 'A | B' /* or */
call show bXOr(a,b) , 'A && B' /* xor */
call show bNot(a) , '¬ A' /* not */
call show bShiftL(a,b) , 'A [«B]' /* shift left */
call show bShiftR(a,b) , 'A [»B]' /* shirt right */
/*┌───────────────────────────────────────────────────────────────┐
Since REXX stores numbers (indeed, all values) as characters,
it makes no sense to "rotate" a value, since there aren't any
boundries for the value. I.E.: there isn't any 32bit word
"container" or "cell" (for instance) to store an integer.
*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines─────────────────────────*/
call show bAnd(a,b) , 'A & B' /* and */
call show bOr( a,b) , 'A | B' /* or */
call show bXOr(a,b) , 'A && B' /* xor */
call show bNot(a) , '¬ A' /* not */
call show bShiftL(a,b) , 'A [«B]' /* shift left */
call show bShiftR(a,b) , 'A [»B]' /* shirt right */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: procedure; parse arg x,t; say right(x,9) center(t,9) right(d2b(x),50); return
d2b: return x2b(d2x(arg(1))) +0 /*some REXXes have the D2B bif.*/
b2d: return x2d(b2x(arg(1))) /*some REXXes have the B2D bif.*/
bNot: return b2d(translate(d2b(arg(1)), 10, 01)) +0 /*+0≡normalize.*/
bShiftL: return (b2d(d2b(arg(1)) || copies(0, arg(2)))) +0
d2b: return x2b(d2x(arg(1))) +0 /*some REXXes have the D2B BIF. */
b2d: return x2d(b2x(arg(1))) /* " " " " B2D " */
bNot: return b2d(translate(d2b(arg(1)), 10, 01)) +0 /*+0 ≡ normalizes the number*/
bShiftL: return (b2d(d2b(arg(1)) || copies(0, arg(2)))) +0 /* " " " " " */
bAnd: procedure; parse arg x,y; return c2d(bitand(d2c(x), d2c(y)))
bOr: procedure; parse arg x,y; return c2d(bitor( d2c(x), d2c(y)))
bXor: procedure; parse arg x,y; return c2d(bitxor(d2c(x), d2c(y)))
/*──────────────────────────────────────────────────────────────────────────────────────*/
bShiftR: procedure; parse arg x,y; $=substr(reverse(d2b(x)), y+1)
if $=='' then $=0; return b2d(reverse($))