September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
82
Task/Bitwise-operations/360-Assembly/bitwise-operations.360
Normal file
82
Task/Bitwise-operations/360-Assembly/bitwise-operations.360
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
* Bitwise operations 15/02/2017
|
||||
BITWISE CSECT
|
||||
USING BITWISE,R13
|
||||
B 72(R15)
|
||||
DC 17F'0'
|
||||
STM R14,R12,12(R13)
|
||||
ST R13,4(R15)
|
||||
ST R15,8(R13)
|
||||
LR R13,R15
|
||||
L R1,A
|
||||
XDECO R1,PG
|
||||
MVC OP,=CL7'A='
|
||||
XPRNT OP,L'OP+L'PG
|
||||
L R1,B
|
||||
XDECO R1,PG
|
||||
MVC OP,=CL7'B='
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* And
|
||||
L R1,A
|
||||
N R1,B
|
||||
XDECO R1,PG
|
||||
MVC OP,=C'A AND B'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* Or
|
||||
L R1,A
|
||||
O R1,B
|
||||
XDECO R1,PG
|
||||
MVC OP,=C'A OR B'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* Xor
|
||||
L R1,A
|
||||
X R1,B
|
||||
XDECO R1,PG
|
||||
MVC OP,=C'A XOR B'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* Not
|
||||
L R1,A
|
||||
X R1,=X'FFFFFFFF' not (by xor -1)
|
||||
XDECO R1,PG
|
||||
MVC OP,=CL7'NOT A'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
*
|
||||
MVC A,=X'80000008' a=-2147483640 (-2^31+8)
|
||||
L R1,A
|
||||
XDECO R1,PG
|
||||
MVC OP,=CL7'A='
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* shift right arithmetic (on 31 bits)
|
||||
L R1,A
|
||||
SRA R1,3
|
||||
XDECO R1,PG
|
||||
MVC OP,=C'A SRA 3'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* shift left arithmetic (on 31 bits)
|
||||
L R1,A
|
||||
SLA R1,3
|
||||
XDECO R1,PG
|
||||
MVC OP,=C'A SLA 3'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* shift right logical (on 32 bits)
|
||||
L R1,A
|
||||
SRL R1,3
|
||||
XDECO R1,PG
|
||||
MVC OP,=C'A SRL 3'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
* shift left logical (on 32 bits)
|
||||
L R1,A
|
||||
SLL R1,3
|
||||
XDECO R1,PG
|
||||
MVC OP,=C'A SLL 3'
|
||||
XPRNT OP,L'OP+L'PG
|
||||
*
|
||||
RETURN L R13,4(0,R13)
|
||||
LM R14,R12,12(R13)
|
||||
XR R15,R15
|
||||
BR R14
|
||||
A DC F'21'
|
||||
B DC F'3'
|
||||
OP DS CL7
|
||||
PG DS CL12
|
||||
YREGS
|
||||
END BITWISE
|
||||
19
Task/Bitwise-operations/ALGOL-W/bitwise-operations.alg
Normal file
19
Task/Bitwise-operations/ALGOL-W/bitwise-operations.alg
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
% performs bitwise and, or, not, left-shift and right shift on the integers n1 and n2 %
|
||||
% Algol W does not have xor, arithmetic right shift, left rotate or right rotate %
|
||||
procedure bitOperations ( integer value n1, n2 ) ;
|
||||
begin
|
||||
bits b1, b2;
|
||||
% the Algol W bitwse operations operate on bits values, so we first convert the %
|
||||
% integers to bits values using the builtin bitstring procedure %
|
||||
% the results are converted back to integers using the builtin number procedure %
|
||||
% all Algol W bits and integers are 32 bits quantities %
|
||||
b1 := bitstring( n1 );
|
||||
b2 := bitstring( n2 );
|
||||
% perform the operaations and display the results as integers %
|
||||
write( n1, " and ", n2, " = ", number( b1 and b2 ) );
|
||||
write( n1, " or ", n2, " = ", number( b1 or b2 ) );
|
||||
write( " "
|
||||
, " not ", n1, " = ", number( not b1 ) );
|
||||
write( n1, " shl ", n2, " = ", number( b1 shl n2 ), " ( left-shift )" );
|
||||
write( n1, " shr ", n2, " = ", number( b1 shr n2 ), " ( right-shift )" )
|
||||
end bitOPerations ;
|
||||
9
Task/Bitwise-operations/BASIC/bitwise-operations-3.basic
Normal file
9
Task/Bitwise-operations/BASIC/bitwise-operations-3.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
org 4084
|
||||
3a 83 40 ld a, (4083)
|
||||
47 ld b, a
|
||||
3a 82 40 ld a, (4082)
|
||||
a0 and b
|
||||
00 nop ; negate and shift instructions take 2 bytes
|
||||
06 00 ld b, 0
|
||||
4f ld c, a ; value in BC reg pair is returned to BASIC
|
||||
c9 ret
|
||||
25
Task/Bitwise-operations/BASIC/bitwise-operations-4.basic
Normal file
25
Task/Bitwise-operations/BASIC/bitwise-operations-4.basic
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
10 REM ABCDEFGHIJKLMNO
|
||||
20 INPUT A
|
||||
30 INPUT B
|
||||
40 POKE 16514,A
|
||||
50 POKE 16515,B
|
||||
60 LET ADDR=16516
|
||||
70 LET R$="3A8340473A8240A00006004FC9"
|
||||
80 POKE ADDR,CODE R$*16+CODE R$(2)-476
|
||||
90 LET R$=R$(3 TO )
|
||||
100 LET ADDR=ADDR+1
|
||||
110 IF R$<>"" THEN GOTO 80
|
||||
120 PRINT A;" AND ";B;" = ";USR 16516
|
||||
130 POKE 16523,176
|
||||
140 PRINT A;" OR ";B;" = ";USR 16516
|
||||
150 POKE 16523,168
|
||||
160 PRINT A;" XOR ";B;" = ";USR 16516
|
||||
170 POKE 16523,237
|
||||
180 POKE 16524,68
|
||||
190 PRINT "NOT ";A;" = ";USR 16516
|
||||
200 POKE 16523,203
|
||||
210 POKE 16524,39
|
||||
220 FOR I=1 TO B
|
||||
230 POKE 16514,USR 16516
|
||||
240 NEXT I
|
||||
250 PRINT A;" << ";B;" = ";PEEK 16514
|
||||
25
Task/Bitwise-operations/Beeswax/bitwise-operations-1.beeswax
Normal file
25
Task/Bitwise-operations/Beeswax/bitwise-operations-1.beeswax
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#eX~T~T_#
|
||||
###>N{` AND `~{~` = `&{Nz1~3J
|
||||
UXe#
|
||||
##>{` OR `~{~` = `|{Nz1~5J
|
||||
UXe#
|
||||
##>{` XOR `~{~` = `${Nz1~7J
|
||||
UXe#
|
||||
##>`NOT `{` = `!{Nz1~9J
|
||||
UXe#
|
||||
##>{` << `~{~` = `({Nz1~9PPJ
|
||||
UXe#
|
||||
##>{` >>> `~{~` = `){` (logical shift right)`N7F+M~1~J
|
||||
UXe#
|
||||
##>{` ROL `~{~` = `[{N7F+P~1~J
|
||||
UXe#
|
||||
##>{` ROR `~{~` = `]{NN8F+P~1~J
|
||||
UXe#
|
||||
##>`Arithmetic shift right is not originally implemented in beeswax.`N q
|
||||
qN`,noitagen yb dezilaer eb nac srebmun evitagen rof RSA ,yllacinhcet tuB`N<
|
||||
##>`logical shift right, and negating the result again:`NN7F++~1~J
|
||||
UXe# #>e#
|
||||
#>~1~[&'pUX{` >> `~{~` = `){` , interpreted as (positive) signed Int64 number (MSB=0), equivalent to >>>`NN;
|
||||
###
|
||||
>UX`-`!P{M!` >> `~{~` = `!)!`-`M!{` , interpreted as (negative) signed Int64 number (MSB=1)`NN;
|
||||
#>e#
|
||||
19
Task/Bitwise-operations/Beeswax/bitwise-operations-2.beeswax
Normal file
19
Task/Bitwise-operations/Beeswax/bitwise-operations-2.beeswax
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
julia> beeswax("Bitops.bswx",0,0.0,Int(20000))
|
||||
i9223653511831486512
|
||||
i48
|
||||
|
||||
9223653511831486512 AND 48 = 48
|
||||
9223653511831486512 OR 48 = 9223653511831486512
|
||||
9223653511831486512 XOR 48 = 9223653511831486464
|
||||
NOT 9223653511831486512 = 9223090561878065103
|
||||
9223653511831486512 << 48 = 13510798882111488
|
||||
9223653511831486512 >>> 48 = 32769 (logical shift right)
|
||||
9223653511831486512 ROL 48 = 13651540665434112
|
||||
9223653511831486512 ROR 48 = 3178497
|
||||
|
||||
Arithmetic shift right is not originally implemented in beeswax.
|
||||
|
||||
But technically, ASR for negative numbers can be realized by negation,
|
||||
logical shift right, and negating the result again:
|
||||
|
||||
-9223090561878065104 >> 48 = -32767 , interpreted as (negative) signed Int64 number (MSB=1)
|
||||
|
|
@ -0,0 +1 @@
|
|||
A>>B = NOT(NOT(A)>>>B)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
A ROL B = A<<(B%64)+A>>>(64-B%64)
|
||||
A ROR B = A>>>(B%64)+A<<(64-B%64)
|
||||
|
|
@ -1,20 +1,19 @@
|
|||
#import system.
|
||||
#import extensions.
|
||||
import extensions.
|
||||
|
||||
#class(extension) testOp
|
||||
extension testOp
|
||||
{
|
||||
#method bitwiseTest : y
|
||||
bitwiseTest : y
|
||||
[
|
||||
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 inverted).
|
||||
console writeLine:self:" shr ":y:" = ":(self shift &index:y).
|
||||
console writeLine:self:" shl ":y:" = ":(self shift &index:(y negative)).
|
||||
console printLine(self," and ",y," = ",self and:y).
|
||||
console printLine(self," or ",y," = ",self or:y).
|
||||
console printLine(self," xor ",y," = ",self xor:y).
|
||||
console printLine("not ",self," = ",self inverted).
|
||||
console printLine(self," shr ",y," = ",self shiftRight:y).
|
||||
console printLine(self," shl ",y," = ",self shiftLeft:y).
|
||||
]
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
program =
|
||||
[
|
||||
console readLine:(Integer new) bitwiseTest:(console readLine:(Integer new)).
|
||||
console readLineTo(Integer new); bitwiseTest(console readLineTo(Integer new)).
|
||||
].
|
||||
|
|
|
|||
|
|
@ -1,18 +1,24 @@
|
|||
import Data.Bits
|
||||
|
||||
bitwise :: Int -> Int -> IO ()
|
||||
bitwise a b = do
|
||||
print $ a .&. b
|
||||
print $ a .|. b
|
||||
print $ a `xor` b
|
||||
print $ complement a
|
||||
print $ shiftL a b -- left shift
|
||||
print $ shiftR a b -- arithmetic right shift
|
||||
print $ shift a b -- You can also use the "unified" shift function; positive is for left shift, negative is for right shift
|
||||
print $ shift a (-b)
|
||||
print $ rotateL a b -- rotate left
|
||||
print $ rotateR a b -- rotate right
|
||||
print $ rotate a b -- You can also use the "unified" rotate function; positive is for left rotate, negative is for right rotate
|
||||
print $ rotate a (-b)
|
||||
bitwise a b =
|
||||
mapM_
|
||||
print
|
||||
[ a .&. b
|
||||
, a .|. b
|
||||
, a `xor` b
|
||||
, complement a
|
||||
, shiftL a b -- left shift
|
||||
, shiftR a b -- arithmetic right shift
|
||||
, shift a b -- You can also use the "unified" shift function;
|
||||
-- positive is for left shift, negative is for right shift
|
||||
, shift a (-b)
|
||||
, rotateL a b -- rotate left
|
||||
, rotateR a b -- rotate right
|
||||
, rotate a b -- You can also use the "unified" rotate function;
|
||||
-- positive is for left rotate, negative is for right rotate
|
||||
, rotate a (-b)
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = bitwise 255 170
|
||||
|
|
|
|||
12
Task/Bitwise-operations/Julia/bitwise-operations.julia
Normal file
12
Task/Bitwise-operations/Julia/bitwise-operations.julia
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# Version 5.2
|
||||
@show 1 & 2 # AND
|
||||
@show 1 | 2 # OR
|
||||
@show 1 ^ 2 # XOR -- for Julia 6.0 the operator is `⊻`
|
||||
@show ~1 # NOT
|
||||
@show 1 >>> 2 # SHIFT RIGHT (LOGICAL)
|
||||
@show 1 >> 2 # SHIFT RIGHT (ARITMETIC)
|
||||
@show 1 << 2 # SHIFT LEFT (ARITMETIC/LOGICAL)
|
||||
|
||||
A = BitArray([true, true, false, false, true])
|
||||
@show A ror(A,1) ror(A,2) ror(A,5) # ROTATION RIGHT
|
||||
@show rol(A,1) rol(A,2) rol(A,5) # ROTATION LEFT
|
||||
21
Task/Bitwise-operations/Kotlin/bitwise-operations.kotlin
Normal file
21
Task/Bitwise-operations/Kotlin/bitwise-operations.kotlin
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* for symmetry with Kotlin's other binary bitwise operators
|
||||
we wrap Java's 'rotate' methods as infix functions */
|
||||
infix fun Int.rol(distance: Int): Int = Integer.rotateLeft(this, distance)
|
||||
infix fun Int.ror(distance: Int): Int = Integer.rotateRight(this, distance)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// inferred type of x and y is Int i.e. 32 bit signed integers
|
||||
val x = 10
|
||||
val y = 2
|
||||
println("x = $x")
|
||||
println("y = $y")
|
||||
println("NOT x = ${x.inv()}")
|
||||
println("x AND y = ${x and y}")
|
||||
println("x OR y = ${x or y}")
|
||||
println("x XOR y = ${x xor y}")
|
||||
println("x SHL y = ${x shl y}")
|
||||
println("x ASR y = ${x shr y}") // arithmetic shift right (sign bit filled)
|
||||
println("x LSR y = ${x ushr y}") // logical shift right (zero filled)
|
||||
println("x ROL y = ${x rol y}")
|
||||
println("x ROR y = ${x ror y}")
|
||||
}
|
||||
18
Task/Bitwise-operations/Maple/bitwise-operations.maple
Normal file
18
Task/Bitwise-operations/Maple/bitwise-operations.maple
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
with(Bits):
|
||||
bit:=proc(A,B)
|
||||
local a,b,c,d,e,f,g,h,i,x,bitpow;
|
||||
bitpow := 2^B:
|
||||
a:=And(A,B);
|
||||
b:=Not(A);
|
||||
c:=Or(A,B);
|
||||
d:=Xor(A,B);
|
||||
#Left Shift
|
||||
e:= irem(2*A,bitpow);
|
||||
#Right Shift
|
||||
f := iquo(A,2);
|
||||
#Left Rotate
|
||||
g:= irem(2*A,bitpow,'x')+x;
|
||||
#Rightarithshift
|
||||
i:= iquo(A,2)+bitpow/2*irem(A,bitpow/2);
|
||||
return a,b,c,d,e,f,g,i;
|
||||
end proc;
|
||||
20
Task/Bitwise-operations/OoRexx/bitwise-operations.rexx
Normal file
20
Task/Bitwise-operations/OoRexx/bitwise-operations.rexx
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ooRexx *************************************************************
|
||||
/ Bit Operations work as in Rexx (of course)
|
||||
* Bit operations are performed up to the length of the shorter string.
|
||||
* The rest of the longer string is copied to the result.
|
||||
* ooRexx introduces the possibility to specify a padding character
|
||||
* to be used for expanding the shorter string.
|
||||
* 10.11.2012 Walter Pachl taken over from REXX and extended for ooRexx
|
||||
**********************************************************************/
|
||||
a=21
|
||||
b=347
|
||||
Say ' a :'c2b(a) ' 'c2x(a)
|
||||
Say ' b :'c2b(b) c2x(b)
|
||||
Say 'bitand(a,b) :'c2b(bitand(a,b)) c2x(bitand(a,b))
|
||||
Say 'bitor(a,b) :'c2b(bitor(a,b)) c2x(bitor(a,b))
|
||||
Say 'bitxor(a,b) :'c2b(bitxor(a,b)) c2x(bitxor(a,b))
|
||||
p='11111111'B
|
||||
Say 'ooRexx only:'
|
||||
Say 'a~bitor(b,p):'c2b(a~bitor(b,p)) c2x(a~bitor(b,p))
|
||||
Exit
|
||||
c2b: return x2b(c2x(arg(1)))
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
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);
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
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);
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ 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).list.rotate(b).reverse] }
|
||||
multi sub infix:<⥀>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).reverse.rotate(b)] }
|
||||
multi sub infix:<⥀>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).reverse.list.rotate(b)] }
|
||||
|
||||
sub int-bits (Int $a, Int $b) {
|
||||
say '';
|
||||
|
|
|
|||
|
|
@ -1,27 +1,23 @@
|
|||
/*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)
|
||||
|
||||
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 */
|
||||
/*REXX program performs bit─wise operations on integers: & | && ¬ «L »R */
|
||||
numeric digits 1000 /*be able to handle ginormous integers.*/
|
||||
say center('decimal', 9) center("value", 9) center('bits', 50)
|
||||
say copies('─' , 9) copies("─" , 9) copies('─', 50)
|
||||
a = 21 ; call show a , 'A' /* display A */
|
||||
b = 3 ; call show b , 'B' /* display 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 */
|
||||
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))) /* " " " " 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($))
|
||||
show: say right( arg(1), 9) center( arg(2), 9) right( d2b( arg(1) ), 50); return
|
||||
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 a #.*/
|
||||
bShiftL: return b2d( d2b( arg(1) ) || copies(0, arg(2) ) ) +0 /* " " " " " */
|
||||
bAnd: return c2d( bitand( d2c( arg(1) ), d2c( arg(2) ) ) )
|
||||
bOr: return c2d( bitor( d2c( arg(1) ), d2c( arg(2) ) ) )
|
||||
bXor: return c2d( bitxor( d2c( arg(1) ), d2c( arg(2) ) ) )
|
||||
bShiftR: $=substr(reverse(d2b(arg(1))),arg(2)+1); if $='' then $=0; return b2d(reverse($))
|
||||
|
|
|
|||
|
|
@ -1,15 +1,10 @@
|
|||
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
|
||||
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)
|
||||
|
|
|
|||
6
Task/Bitwise-operations/VBA/bitwise-operations-1.vba
Normal file
6
Task/Bitwise-operations/VBA/bitwise-operations-1.vba
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Debug.Print Hex(&HF0F0 And &HFF00) 'F000
|
||||
Debug.Print Hex(&HF0F0 Or &HFF00) 'FFF0
|
||||
Debug.Print Hex(&HF0F0 Xor &HFF00) 'FF0
|
||||
Debug.Print Hex(Not &HF0F0) 'F0F
|
||||
Debug.Print Hex(&HF0F0 Eqv &HFF00) 'F00F
|
||||
Debug.Print Hex(&HF0F0 Imp &HFF00) 'FF0F
|
||||
79
Task/Bitwise-operations/VBA/bitwise-operations-2.vba
Normal file
79
Task/Bitwise-operations/VBA/bitwise-operations-2.vba
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
Function MaskL(k As Integer) As Long
|
||||
If k < 1 Then
|
||||
MaskL = 0
|
||||
ElseIf k > 31 Then
|
||||
MaskL = -1
|
||||
Else
|
||||
MaskL = (-1) Xor (2 ^ (32 - k) - 1)
|
||||
End If
|
||||
End Function
|
||||
Function MaskR(k As Integer) As Long
|
||||
If k < 1 Then
|
||||
MaskR = 0
|
||||
ElseIf k > 31 Then
|
||||
MaskR = -1
|
||||
Else
|
||||
MaskR = 2 ^ k - 1
|
||||
End If
|
||||
End Function
|
||||
Function Bit(k As Integer) As Long
|
||||
If k < 0 Or k > 31 Then
|
||||
Bit = 0
|
||||
ElseIf k = 31 Then
|
||||
Bit = MaskL(1)
|
||||
Else
|
||||
Bit = 2 ^ k
|
||||
End If
|
||||
End Function
|
||||
Function ShiftL(n As Long, k As Integer) As Long
|
||||
If k = 0 Then
|
||||
ShiftL = n
|
||||
ElseIf k > 31 Then
|
||||
ShiftL = 0
|
||||
ElseIf k < 0 Then
|
||||
ShiftL = ShiftR(n, -k)
|
||||
Else
|
||||
ShiftL = (n And MaskR(31 - k)) * 2 ^ k
|
||||
If (n And Bit(31 - k)) <> 0 Then ShiftL = ShiftL Or MaskL(1)
|
||||
End If
|
||||
End Function
|
||||
Function ShiftR(n As Long, k As Integer) As Long
|
||||
If k = 0 Then
|
||||
ShiftR = n
|
||||
ElseIf k > 31 Then
|
||||
ShiftR = 0
|
||||
ElseIf k < 0 Then
|
||||
ShiftR = ShiftL(n, -k)
|
||||
Else
|
||||
ShiftR = (n And MaskR(31)) \ 2 ^ k
|
||||
If (n And MaskL(1)) <> 0 Then ShiftR = ShiftR Or Bit(31 - k)
|
||||
End If
|
||||
End Function
|
||||
Function RotateL(n As Long, k As Integer) As Long
|
||||
k = (32768 + k) Mod 32
|
||||
If k = 0 Then
|
||||
RotateL = n
|
||||
Else
|
||||
RotateL = ShiftL(n, k) Or ShiftR(n, 32 - k)
|
||||
End If
|
||||
End Function
|
||||
Function RotateR(n As Long, k As Integer) As Long
|
||||
k = (32768 + k) Mod 32
|
||||
If k = 0 Then
|
||||
RotateR = n
|
||||
Else
|
||||
RotateR = ShiftR(n, k) Or ShiftL(n, 32 - k)
|
||||
End If
|
||||
End Function
|
||||
Function ClearBit(n As Long, k As Integer) As Long
|
||||
ClearBit = n And Not Bit(k)
|
||||
End Function
|
||||
Function SetBit(n As Long, k As Integer) As Long
|
||||
SetBit = n Or Bit(k)
|
||||
End Function
|
||||
Function SwitchBit(n As Long, k As Integer) As Long
|
||||
SwitchBit = n Xor Bit(k)
|
||||
End Function
|
||||
Function TestBit(n As Long, k As Integer) As Boolean
|
||||
TestBit = (n And Bit(k)) <> 0
|
||||
End Function
|
||||
11
Task/Bitwise-operations/VBA/bitwise-operations-3.vba
Normal file
11
Task/Bitwise-operations/VBA/bitwise-operations-3.vba
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Debug.Print Hex(MaskL(8)) 'FF000000
|
||||
Debug.Print Hex(MaskR(8)) 'FF
|
||||
Debug.Print Hex(Bit(7)) '80
|
||||
Debug.Print Hex(ShiftL(-1, 8)) 'FFFFFF00
|
||||
Debug.Print Hex(ShiftL(-1, -8)) 'FFFFFF
|
||||
Debug.Print Hex(ShiftR(-1, 8)) 'FFFFFF
|
||||
Debug.Print Hex(ShiftR(-1, -8)) 'FFFFFF00
|
||||
Debug.Print Hex(RotateL(65535, 8)) 'FFFF00
|
||||
Debug.Print Hex(RotateL(65535, -8)) 'FF0000FF
|
||||
Debug.Print Hex(RotateR(65535, 8)) 'FF0000FF
|
||||
Debug.Print Hex(RotateR(65535, -8)) 'FFFF00
|
||||
104
Task/Bitwise-operations/X86-Assembly/bitwise-operations.x86
Normal file
104
Task/Bitwise-operations/X86-Assembly/bitwise-operations.x86
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
extern printf
|
||||
global main
|
||||
|
||||
section .text
|
||||
main
|
||||
mov eax, dword [_a]
|
||||
mov ecx, dword [_b]
|
||||
push ecx
|
||||
push eax
|
||||
|
||||
and eax, ecx
|
||||
mov ebx, _opand
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
or eax, ecx
|
||||
mov ebx, _opor
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
xor eax, ecx
|
||||
mov ebx, _opxor
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
shr eax, cl
|
||||
mov ebx, _opshr
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
shl eax, cl
|
||||
mov ebx, _opshl
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
rol eax, cl
|
||||
mov ebx, _oprol
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
ror eax, cl
|
||||
mov ebx, _opror
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
sal eax, cl
|
||||
mov ebx, _opsal
|
||||
call out_ops
|
||||
|
||||
call get_nums
|
||||
sar eax, cl
|
||||
mov ebx, _opsar
|
||||
call out_ops
|
||||
|
||||
mov eax, dword [esp+0]
|
||||
not eax
|
||||
push eax
|
||||
not eax
|
||||
push eax
|
||||
push _opnot
|
||||
push _null
|
||||
push _testn
|
||||
call printf
|
||||
add esp, 20
|
||||
|
||||
add esp, 8
|
||||
ret
|
||||
|
||||
out_ops
|
||||
push eax
|
||||
push ecx
|
||||
push ebx
|
||||
push dword [_a]
|
||||
push _test
|
||||
call printf
|
||||
add esp, 20
|
||||
ret
|
||||
|
||||
get_nums
|
||||
mov eax, dword [esp+4]
|
||||
mov ecx, dword [esp+8]
|
||||
ret
|
||||
|
||||
section .data
|
||||
|
||||
_a dd 11
|
||||
_b dd 3
|
||||
|
||||
section .rodata
|
||||
_test db '%08x %s %08x = %08x', 10, 0
|
||||
_testn db '%08s %s %08x = %08x', 10, 0
|
||||
_opand db 'and', 0
|
||||
_opor db 'or ', 0
|
||||
_opxor db 'xor', 0
|
||||
_opshl db 'shl', 0
|
||||
_opshr db 'shr', 0
|
||||
_opror db 'ror', 0
|
||||
_oprol db 'rol', 0
|
||||
_opnot db 'not', 0
|
||||
_opsal db 'sal', 0
|
||||
_opsar db 'sar', 0
|
||||
_null db 0
|
||||
|
||||
end
|
||||
8
Task/Bitwise-operations/XLISP/bitwise-operations.xlisp
Normal file
8
Task/Bitwise-operations/XLISP/bitwise-operations.xlisp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(defun bitwise-operations (a b)
|
||||
; rotate operations are not supported
|
||||
(print `(,a and ,b = ,(logand a b)))
|
||||
(print `(,a or ,b = ,(logior a b)))
|
||||
(print `(,a xor ,b = ,(logxor a b)))
|
||||
(print `(,a left shift by ,b = ,(lsh a b)))
|
||||
(print `(,a right shift by ,b = ,(lsh a (- b)))) ; negative second operand shifts right
|
||||
(print `(,a arithmetic right shift by ,b = ,(ash a (- b)))) )
|
||||
8
Task/Bitwise-operations/Zkl/bitwise-operations.zkl
Normal file
8
Task/Bitwise-operations/Zkl/bitwise-operations.zkl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(7).bitAnd(1) //-->1
|
||||
(8).bitOr(1) //-->9
|
||||
(7).bitXor(1) //-->6
|
||||
(1).bitNot() : "%,x".fmt(_) //-->ff|ff|ff|ff|ff|ff|ff|fe
|
||||
(7).shiftRight(1) //-->3
|
||||
(7).shiftLeft(1) //-->0xe
|
||||
(-1).toString(16) //-->ffffffffffffffff
|
||||
(-1).shiftRight(1).toString(16) //-->7fffffffffffffff
|
||||
Loading…
Add table
Add a link
Reference in a new issue