python - can this work with other bit wise gates? -
i have been working on bit of bit wise gates.
a=0b01100001 b=0b01100010 bin((a ^ 0b11111111) & (b ^ 0b11111111)) >>>0b10011100
so wondering how adjust work nand gate , other instance.
>>> bin((a ~ 0b11111111)& (b ~ 0b11111111)) syntaxerror: invalid syntax >>> bin((a ^ 0b11111111) ~ (b ^ 0b11111111)) syntaxerror: invalid syntax
does not work example.
you should start here: https://wiki.python.org/moin/bitwiseoperators
x << y
returns x bits shifted left y places (and new bits on right-hand-side zeros). same multiplying x 2**y.x >> y
returns x bits shifted right y places. same //'ing x 2**y.x & y
"bitwise and". each bit of output 1 if corresponding bit of x , of y 1, otherwise it's 0.x | y
"bitwise or". each bit of output 0 if corresponding bit of x , of y 0, otherwise it's 1.~ x
returns complement of x - number switching each 1 0 , each 0 1. same -x - 1.x ^ y
"bitwise exclusive or". each bit of output same corresponding bit in x if bit in y 0, , it's complement of bit in x if bit in y 1.
according wikipedia:
the function nand(a1, a2, ..., an) logically equivalent not(a1 , a2 , ... , an).
so in order make bitwise nand operation on operands a
, b
in python:
~(a & b)
which can put function:
bnand = lambda a, b: ~(a & b)
the corresponding non-bitwise nand be:
not (a , b)
so make sure don't confuse and
, &
(as not
, ~
); also, logic operators lower case.
p.s. also, make sure read this: python: unsigned 32 bit bitwise arithmetic
Comments
Post a Comment