This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,6 @@
10 3
\$@$@$@$@\ { 3 copies }
"a & b = "&."
a | b = "|."
~a = "%~."
"

View file

@ -0,0 +1,9 @@
"a=" "b=" [ write readln string>number ] bi@
{
[ bitand "a AND b: " write . ]
[ bitor "a OR b: " write . ]
[ bitxor "a XOR b: " write . ]
[ drop bitnot "NOT a: " write . ]
[ abs shift "a asl b: " write . ]
[ neg shift "a asr b: " write . ]
} 2cleave

View file

@ -0,0 +1,8 @@
a=255
b=5
a AND b: 5
a OR b: 255
a XOR b: 250
NOT a: -256
a asl b: 8160
a asr b: 7

View file

@ -0,0 +1,11 @@
def bitwise = { a, b ->
println """
a & b = ${a} & ${b} = ${a & b}
a | b = ${a} | ${b} = ${a | b}
a ^ b = ${a} ^ ${b} = ${a ^ b}
~ a = ~ ${a} = ${~ a}
a << b = ${a} << ${b} = ${a << b}
a >> b = ${a} >> ${b} = ${a >> b} arithmetic (sign-preserving) shift
a >>> b = ${a} >>> ${b} = ${a >>> b} logical (zero-filling) shift
"""
}

View file

@ -0,0 +1 @@
bitwise(-15,3)

View file

@ -0,0 +1,4 @@
i = IAND(k, j)
i = IOR( k, j)
i = IEOR(k, j)
i = NOT( k )

View file

@ -0,0 +1,21 @@
procedure main()
bitdemo(255,2)
bitdemo(-15,3)
end
procedure bitdemo(i,i2)
write()
demowrite("i",i)
demowrite("i2",i2)
demowrite("complement i",icom(i))
demowrite("i or i2",ior(i,i2))
demowrite("i and i2",iand(i,i2))
demowrite("i xor i2",ixor(i,i2))
demowrite("i shift " || i2,ishift(i,i2))
demowrite("i shift -" || i2,ishift(i,-i2))
return
end
procedure demowrite(vs,v)
return write(vs, ": ", v, " = ", int2bit(v),"b")
end

View file

@ -0,0 +1,15 @@
[ bitwise a b temp;
print "a and b: ", a & b, "^";
print "a or b: ", a | b, "^";
print "not a: ", ~a, "^";
@art_shift a b -> temp;
print "a << b (arithmetic): ", temp, "^";
temp = -b;
@art_shift a temp -> temp;
print "a >> b (arithmetic): ", temp, "^";
@log_shift a b -> temp;
print "a << b (logical): ", temp, "^";
temp = -b;
@log_shift a temp -> temp;
print "a >> b (logical): ", temp, "^";
];

View file

@ -0,0 +1,9 @@
bAND=: 17 b. NB. 16+#.0 0 0 1
bOR=: 23 b. NB. 16+#.0 1 1 1
bXOR=: 22 b. NB. 16+#.0 1 1 0
b1NOT=: 28 b. NB. 16+#.1 1 0 0
bLshift=: 33 b.~ NB. see http://www.jsoftware.com/help/release/bdot.htm
bRshift=: 33 b.~ -
bRAshift=: 34 b.~ -
bLrot=: 32 b.~
bRrot=: 32 b.~ -

View file

@ -0,0 +1,4 @@
bitwise=: 1 :0
:
smoutput (((":x),"1' ',.(>u),.' '),"1":y),"1' => ',"1'.X'{~#:x u`:0 y
)

View file

@ -0,0 +1,10 @@
254 bAND`bOR`bXOR`b1NOT`bLshift`bRshift`bRAshift`bLrot`bRrot bitwise 3
254 bAND 3 => ............................X.
254 bOR 3 => ......................XXXXXXXX
254 bXOR 3 => ......................XXXXXX.X
254 b1NOT 3 => XXXXXXXXXXXXXXXXXXXXXX.......X
254 bLshift 3 => ...................XXXXXXX....
254 bRshift 3 => .........................XXXXX
254 bRAshift 3 => .........................XXXXX
254 bLrot 3 => ...................XXXXXXX....
254 bRrot 3 => .........................XXXXX

View file

@ -0,0 +1,2 @@
bXOR/ 3333 5555 7777 9999
8664

View file

@ -0,0 +1,60 @@
; ModuleID = 'test.o'
;e means little endian
;p: { pointer size : pointer abi : preferred alignment for pointers }
;i same for integers
;v is for vectors
;f for floats
;a for aggregate types
;s for stack objects
;n: {size:size:size...}, best integer sizes
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
;this was compiled with mingw32; thus it must be linked to an ABI compatible c library
target triple = "i386-mingw32"
@.str = private constant [13 x i8] c"a and b: %d\0A\00", align 1 ; <[13 x i8]*> [#uses=1]
@.str1 = private constant [12 x i8] c"a or b: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1]
@.str2 = private constant [13 x i8] c"a xor b: %d\0A\00", align 1 ; <[13 x i8]*> [#uses=1]
@.str3 = private constant [11 x i8] c"not a: %d\0A\00", align 1 ; <[11 x i8]*> [#uses=1]
@.str4 = private constant [12 x i8] c"a << n: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1]
@.str5 = private constant [12 x i8] c"a >> n: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1]
@.str6 = private constant [12 x i8] c"c >> b: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1]
;A function that will do many bitwise opreations to two integer arguments, %a and %b
define void @bitwise(i32 %a, i32 %b) nounwind {
;entry block
entry:
;Register to store (a & b)
%0 = and i32 %b, %a ; <i32> [#uses=1]
;print the results
%1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str, i32 0, i32 0), i32 %0) nounwind ; <i32> [#uses=0]
;Register to store (a | b)
%2 = or i32 %b, %a ; <i32> [#uses=1]
;print the results
%3 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str1, i32 0, i32 0), i32 %2) nounwind ; <i32> [#uses=0]
;Register to store (a ^ b)
%4 = xor i32 %b, %a ; <i32> [#uses=1]
;print the results
%5 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str2, i32 0, i32 0), i32 %4) nounwind ; <i32> [#uses=0]
;Register to store (~a)
%not = xor i32 %a, -1 ; <i32> [#uses=1]
;print the results
%6 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([11 x i8]* @.str3, i32 0, i32 0), i32 %not) nounwind ; <i32> [#uses=0]
;Register to store (a << b)
%7 = shl i32 %a, %b ; <i32> [#uses=1]
;print the results
%8 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str4, i32 0, i32 0), i32 %7) nounwind ; <i32> [#uses=0]
;Register to store (a >> b) (a is signed)
%9 = ashr i32 %a, %b ; <i32> [#uses=1]
;print the results
%10 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str5, i32 0, i32 0), i32 %9) nounwind ; <i32> [#uses=0]
;Register to store (c >> b), where c is unsiged (eg. logical right shift)
%11 = lshr i32 %a, %b ; <i32> [#uses=1]
;print the results
%12 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str6, i32 0, i32 0), i32 %11) nounwind ; <i32> [#uses=0]
;terminator instruction
ret void
}
;Declare external fuctions
declare i32 @printf(i8* nocapture, ...) nounwind

View file

@ -0,0 +1,11 @@
over : 2 pick
2dup : over over
bitwise : \
" A=" ,t over ,h sp " B=" ,t dup ,h nl \
" A and B=" ,t 2dup & ,h nl \
" A or B=" ,t 2dup | ,h nl \
" A xor B=" ,t over ^ ,h nl \
" not A=" ,t ~ ,h nl
\ a \ 7 bitwise # hex literals

View file

@ -0,0 +1,46 @@
' bitwise operations on byte-sized variables
v =int( 256 *rnd( 1))
s = 1
print "Shift ="; s; " place."
print
print "Number as dec. = "; v; " & as 8-bits byte = ", dec2Bin$( v)
print "NOT as dec. = "; bitInvert( v), dec2Bin$( bitInvert( v))
print "Shifted left as dec. = "; shiftLeft( v, s), dec2Bin$( shiftLeft( v, s))
print "Shifted right as dec. = "; shiftRight( v, s), dec2Bin$( shiftRight( v, s))
print "Rotated left as dec. = "; rotateLeft( v, s), dec2Bin$( rotateLeft( v, s))
print "Rotated right as dec. = "; rotateRight( v, s), dec2Bin$( rotateRight( v, s))
end
function shiftLeft( b, n)
shiftLeft =( b *2^n) and 255
end function
function shiftRight( b, n)
shiftRight =int(b /2^n)
end function
function rotateLeft( b, n)
rotateLeft = (( 2^n *b) mod 256) or ( b >127)
end function
function rotateRight( b, n)
rotateRight = (128*( b and 1)) or int( b /2)
end function
function bitInvert( b)
bitInvert =b xor 255
end function
function dec2Bin$( num) ' Given an integer decimal 0 <--> 255, returns binary equivalent as a string
n =num
dec2Bin$ =""
while ( num >0)
dec2Bin$ =str$( num mod 2) +dec2Bin$
num =int( num /2)
wend
dec2Bin$ =right$( "00000000" +dec2Bin$, 8)
end function

View file

@ -0,0 +1,11 @@
to bitwise :a :b
(print [a and b:] BitAnd :a :b)
(print [a or b:] BitOr :a :b)
(print [a xor b:] BitXor :a :b)
(print [not a:] BitNot :a)
; shifts are to the left if positive, to the right if negative
(print [a lshift b:] LShift :a :b)
(print [a lshift -b:] LShift :a minus :b)
(print [-a ashift -b:] AShift minus :a minus :b)
end
bitwise 255 5

View file

@ -0,0 +1,7 @@
a and b: 5
a or b: 255
a xor b: 250
not a: -256
a lshift b: 8160
a lshift -b: 7
-a ashift -b: -8

View file

@ -0,0 +1,11 @@
fn bitwise a b =
(
format "a and b: %\n" (bit.and a b)
format "a or b: %\n" (bit.or a b)
format "a xor b: %\n" (bit.xor a b)
format "not a: %\n" (bit.not a)
format "Left shift a: %\n" (bit.shift a b)
format "Right shift a: %\n" (bit.shift a -b)
)
bitwise 255 170

View file

@ -0,0 +1,15 @@
MCSKIP "WITH" NL
"" Bitwise operations
"" assumes macros on input stream 1, terminal on stream 2
MCSKIP MT,<>
MCINS %.
MCDEF SL SPACES NL AS <MCSET T1=%A1.
MCSET T2=%A2.
a and b = %%T1.&%T2..
a or b = %%T1.|%T2..
The other operators are not supported.
MCSET S10=0
>
MCSKIP SL WITH *
MCSET S1=1
*MCSET S10=2

View file

@ -0,0 +1,18 @@
(*and xor and or*)
BitAnd[integer1, integer2]
BitXor[integer1, integer2]
BitOr[integer1, integer2]
(*logical not*)
BitNot[integer1]
(*left and right shift*)
BitShiftLeft[integer1]
BitShiftRight[integer1]
(*rotate digits left and right*)
FromDigits[RotateLeft[IntegerDigits[integer1, 2]], 2]
FromDigits[RotateRight[IntegerDigits[integer1, 2]], 2]
(*right arithmetic shift*)
FromDigits[Prepend[Most[#], #[[1]]], 2] &[IntegerDigits[integer1, 2]]

View file

@ -0,0 +1 @@
BitXor[3333, 5555, 7777, 9999]

View file

@ -0,0 +1,23 @@
load(functs)$
a: 3661$
b: 2541$
logor(a, b);
/* 4077 */
logand(a, b);
/* 2125 */
logxor(a, b);
/* 1952 */
/* NOT(x) is simply -x - 1
-a - 1;
/* -3662 */
logor(a, -a - 1);
/* -1 */
logand(a, -a - 1);
/* 0 */

View file

@ -0,0 +1,22 @@
MODULE Bitwise EXPORTS Main;
IMPORT IO, Fmt, Word;
VAR c: Word.T;
PROCEDURE Bitwise(a, b: INTEGER) =
BEGIN
IO.Put("a AND b: " & Fmt.Int(Word.And(a, b)) & "\n");
IO.Put("a OR b: " & Fmt.Int(Word.Or(a, b)) & "\n");
IO.Put("a XOR b: " & Fmt.Int(Word.Xor(a, b)) & "\n");
IO.Put("NOT a: " & Fmt.Int(Word.Not(a)) & "\n");
c := a;
IO.Put("c LeftShift b: " & Fmt.Unsigned(Word.LeftShift(c, b)) & "\n");
IO.Put("c RightShift b: " & Fmt.Unsigned(Word.RightShift(c, b)) & "\n");
IO.Put("c LeftRotate b: " & Fmt.Unsigned(Word.LeftRotate(c, b)) & "\n");
IO.Put("c RightRotate b: " & Fmt.Unsigned(Word.RightRotate(c, b)) & "\n");
END Bitwise;
BEGIN
Bitwise(255, 5);
END Bitwise.