Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
10
Task/Bitwise-operations/Babel/bitwise-operations-1.pb
Normal file
10
Task/Bitwise-operations/Babel/bitwise-operations-1.pb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
((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 <<}))
|
||||
11
Task/Bitwise-operations/Babel/bitwise-operations-2.pb
Normal file
11
Task/Bitwise-operations/Babel/bitwise-operations-2.pb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
1
|
||||
d
|
||||
fffffff7
|
||||
c
|
||||
fffffff3
|
||||
a00
|
||||
0
|
||||
0
|
||||
a00
|
||||
2800000
|
||||
fffffffa
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
T rot(T)(in T x, in int shift) pure nothrow {
|
||||
T rot(T)(in T x, in int shift) pure nothrow @nogc {
|
||||
return (x >>> shift) | (x << (T.sizeof * 8 - shift));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@
|
|||
console write:x write:" or " write:y write:" = " writeLine:(x or:y).
|
||||
console write:x write:" xor " write:y write:" = " writeLine:(x xor:y).
|
||||
console write:"not " write:x write:" = " writeLine:(x not).
|
||||
console write:x write:" shr " write:y write:" = " writeLine:(x shift:y).
|
||||
console write:x write:" shl " write:y write:" = " writeLine:(x shift:(y negative)).
|
||||
console write:x write:" shr " write:y write:" = " writeLine:(x shift &index:(y int)).
|
||||
console write:x write:" shl " write:y write:" = " writeLine:(x shift &index:(y negative int)).
|
||||
].
|
||||
|
||||
#symbol program =
|
||||
|
|
|
|||
|
|
@ -1,9 +1,15 @@
|
|||
function bitwise($a, $b)
|
||||
{
|
||||
echo '$a AND $b: ', $a & $b, "\n";
|
||||
echo '$a OR $b: ', $a | $b, "\n";
|
||||
echo '$a XOR $b: ', $a ^ $b, "\n";
|
||||
echo 'NOT $a: ', ~$a, "\n";
|
||||
echo '$a << $b: ', $a << $b, "\n"; // left shift
|
||||
echo '$a >> $b: ', $a >> $b, "\n"; // arithmetic right shift
|
||||
function zerofill($a,$b) {
|
||||
if($a>=0) return $a>>$b;
|
||||
if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1); // this line shifts a 0 into the sign bit for compatibility, replace with "if($b==0) return $a;" if you need $b=0 to mean that nothing happens
|
||||
return ((~$a)>>$b)^(0x7fffffff>>($b-1));
|
||||
|
||||
echo '$a AND $b: ' . $a & $b . '\n';
|
||||
echo '$a OR $b: ' . $a | $b . '\n';
|
||||
echo '$a XOR $b: ' . $a ^ $b . '\n';
|
||||
echo 'NOT $a: ' . ~$a . '\n';
|
||||
echo '$a << $b: ' . $a << $b . '\n'; // left shift
|
||||
echo '$a >> $b: ' . $a >> $b . '\n'; // arithmetic right shift
|
||||
echo 'zerofill($a, $b): ' . zerofill($a, $b) . '\n'; // logical right shift
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ 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 = isll(i,n); /* unsigned shifts i left by n places. */
|
||||
k = isrl(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. */
|
||||
|
||||
|
|
@ -17,3 +17,9 @@ u = s & t; /* logical and */
|
|||
u = s | t; /* logical or */
|
||||
u = ^ s; /* logical not */
|
||||
u = s ^ t; /* exclusive or */
|
||||
|
||||
Built-in rotate functions are not available.
|
||||
They can be readily implemented by the user, though:
|
||||
|
||||
u = substr(s, length(s), 1) || substr(s, 1, length(s)-1); /* implements rotate right. */
|
||||
u = substr(s, 2) || substr(s, 1, 1); /* implements rotate left. */
|
||||
|
|
|
|||
28
Task/Bitwise-operations/Perl-6/bitwise-operations.pl6
Normal file
28
Task/Bitwise-operations/Perl-6/bitwise-operations.pl6
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
constant MAXINT = uint.Range.max;
|
||||
constant BITS = MAXINT.base(2).chars;
|
||||
|
||||
# define rotate ops for the fun of it
|
||||
multi sub infix:<⥁>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).rotate(b).reverse] }
|
||||
multi sub infix:<⥀>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).reverse.rotate(b)] }
|
||||
|
||||
sub int-bits (Int $a, Int $b) {
|
||||
say '';
|
||||
say_bit "$a", $a;
|
||||
say '';
|
||||
say_bit "2's complement $a", +^$a;
|
||||
say_bit "$a and $b", $a +& $b;
|
||||
say_bit "$a or $b", $a +| $b;
|
||||
say_bit "$a xor $b", $a +^ $b;
|
||||
say_bit "$a unsigned shift right $b", ($a +& MAXINT) +> $b;
|
||||
say_bit "$a signed shift right $b", $a +> $b;
|
||||
say_bit "$a rotate right $b", $a ⥁ $b;
|
||||
say_bit "$a shift left $b", $a +< $b;
|
||||
say_bit "$a rotate left $b", $a ⥀ $b;
|
||||
}
|
||||
|
||||
int-bits(7,2);
|
||||
int-bits(-65432,31);
|
||||
|
||||
sub say_bit ($message, $value) {
|
||||
printf("%30s: %{'0' ~ BITS}b\n", $message, $value +& MAXINT);
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*REXX program performs bitwise operations on integers: & | && ¬ «L »R */
|
||||
numeric digits 1000 /*be able to handle bit integers.*/
|
||||
numeric digits 1000 /*be able to handle big integers.*/
|
||||
|
||||
say center('decimal',9) center("value",9) center('bits',50)
|
||||
say copies('─',9) copies('─',9) copies('─',50)
|
||||
|
|
@ -13,7 +13,6 @@ b = 3 ; call show b , 'B' /* show & tell B */
|
|||
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 │
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
(import (rnrs arithmetic bitwise (6)))
|
||||
|
||||
(define (bitwise a b)
|
||||
(display (bitwise-and a b))
|
||||
(newline)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue