I understand the symbols. I want to know how to perform the task in a script
or terminal. I have searched Google, but I never saw a command. Typing "101
& 010" or "x = (int(101, 2) & int(010, 2))" only gives errors.
Your problem here isn't in the bitwise operators, but in your binary
literals. Python deliberately and consciously rejects 010 as a
literal, because it might be interpreted either as decimal 10, or as
octal (decimal 8), the latter being C's interpretation. Fixing that
shows up a more helpful error:
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
x = (int(101, 2) & int(10, 2))
TypeError: int() can't convert non-string with explicit base
The int() call isn't doing what you think it is, because 101 is
already an int. The obvious solution now is to quote the values:
0
But there's an easier way:
0
I think that might do what you want. Also check out the bin()
function, which will turn an integer into a string of digits.
ChrisA