I got two objects, a
and b
, each containing a single byte in a bytes object.
I am trying to do a bitwise operation on this to get the two most significant bits (big-endian, so to the left).
a = sock.recv(1)
b = b'\xc0'
c = a & b
However, it angrily spits a TypeError
in my face.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'bytes' and 'bytes'
Is there any way I can perform an AND operation on the two bytes without having to think of the host system's endianness?
bytes
is defined as an immutable sequence of integers, meaning it's a list-like (basically aconst char[]
). Doing a bitwise&
on a list-like wouldn't really be sensical. – Colombic = (int.from_bytes(a, 'big') & int.from_bytes(b, 'big')).to_bytes(max(len(a), len(b)), 'big')
. – Swann