How do I do a bitwise Not operation in Python?

后端 未结 5 1869
自闭症患者
自闭症患者 2020-12-05 23:10

In order to test building an Xor operation with more basic building blocks (using Nand, Or, and And in my case) I need to be able to do a Not operation. The built-in n

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 23:40

    Another way to achieve this, is to assign a mask like this (should be all 1's):

    mask = 0b1111
    

    Then xor it with your number like this:

    number = 0b1100
    mask = 0b1111
    print(bin(number ^ mask))
    

    You can refer the xor truth table to know why it works.

提交回复
热议问题