-1

I have been working on this bit of bit wise gates.

a=0b01100001
b=0b01100010

bin((a ^ 0b11111111) & (b ^ 0b11111111))

>>>0b10011100

So I was wondering how would you adjust this to work for nand gate and any other for that instance.

>>> bin((a ~ 0b11111111)& (b ~ 0b11111111))
SyntaxError: invalid syntax

>>> bin((a ^ 0b11111111) ~ (b ^ 0b11111111))
SyntaxError: invalid syntax

does not work for example.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
user2849377
  • 75
  • 1
  • 2
  • 7
  • There is no NAND bitoperation according to [Python wiki](https://wiki.python.org/moin/BitwiseOperators). `~` is just negation. You could use `~(a AND b)` in Your case. – Kamiccolo Oct 06 '13 at 17:21
  • @Kamiccolo, `~(a & b)` - `&` is bitwise, `AND` isn't Python, `and` is a short-circuiting boolean control structure ;-) – Tim Peters Oct 06 '13 at 17:23
  • so **bin(a AND b)** if values are given surely not?? – user2849377 Oct 06 '13 at 17:24
  • @TimPeters Peters, sorry, that's what I meant :) `~(a & b)` – Kamiccolo Oct 06 '13 at 17:26
  • bin((a=0b11111111) & (b=0b11111111) – user2849377 Oct 06 '13 at 17:27
  • @user2849377, no, again: `AND` is not Python; `and` is incorrect in this context; `&` is Python's binary "and" operator, as you already know. – Tim Peters Oct 06 '13 at 17:27
  • @user2849377, I have no idea what you mean by `bin((a 0b11111111) & (b 0b11111111)` - and neither does Python. `a 0b11111111` all on its own is not Python syntax. – Tim Peters Oct 06 '13 at 17:29
  • so what would you change from **bin((a ^ 0b11111111) & (a ^ 0b11111111))** to make it work for other gates – user2849377 Oct 06 '13 at 17:29
  • @user2849377: please read the answer Erik Allik just gave you. You have to learn Python's syntax first. Then everything is easy :-) – Tim Peters Oct 06 '13 at 17:31
  • In your first question (http://stackoverflow.com/questions/19197495/how-to-do-a-bitwise-nor-gate-in-python-editing-python-maths-to-work-for-me) many users already gave you relevant websites in the comments. Please study a bit before posting. Quoting one of the comment: "And perhaps wiki.python.org/moin/BitwiseOperators and wiki.python.org/moin/BitManipulation are of interest." – Maxime Chéramy Oct 07 '13 at 06:41

1 Answers1

2

You should start from here: https://wiki.python.org/moin/BitwiseOperators

  • x << y
    Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.
  • x >> y
    Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.
  • x & y
    Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.
  • x | y
    Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1.
  • ~ x
    Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.
  • x ^ y
    Does a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.

According to Wikipedia:

The function NAND(a1, a2, ..., an) is logically equivalent to NOT(a1 AND a2 AND ... AND an).

So in order to make a bitwise NAND operation over operands a and b in Python:

~(a & b)

which you can put into a function:

bnand = lambda a, b: ~(a & b)

The corresponding non-bitwise NAND would be:

not (a and b)

so make sure you don't confuse and and & (as well as not and ~); also, the logic operators are lower case.

P.S. Also, make sure you read this: Python: unsigned 32 bit bitwise arithmetic

Community
  • 1
  • 1
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111