How to do a bitwise NOR Gate in Python (editing python maths to work for me)

后端 未结 2 509
南旧
南旧 2020-12-10 23:12

Say I was to write this:

a=01100001 
b=01100010 
c=01100011 
d=01100100 
e=01100101 

each letter resembles the given numbers now how would

2条回答
  •  粉色の甜心
    2020-12-10 23:56

    You gave us the "truth table" of all possible inputs (thanks for that). And you say that the output should be 1 if both inputs are 0, otherwise the output should be 0. The name of that logical operation is NOR, i.e. the negation of OR.

    Note that your inputs are base 10 numbers, but they appear to represent base 2 numbers, or bitsets. So perhaps the first thing we should do is convert them from their base 10 form to base 2. A simple (but not overly efficient) way would be int(str(a), 2).

    From there, it's just a matter of doing the NOR operation on the numbers. From here: https://wiki.python.org/moin/BitwiseOperators it looks like you can do ~(x|y) (negated OR, bitwise).

提交回复
热议问题