Question: I have a base64 encoded bit stream, I want to work with. After decoding it with `base64.b64decode` I get a bytes object (Py3k btw) containing the decoded code. The problem is now, that I would like to work on that bytes object with bit-wise operations, such as shifting, bit wise `and` etc, but that is not possible as it is a bytes sequence.

Is there any way to use binary operations on that decoded code?

Answer: 
If you need to make your `bytes` object mutable then you can construct a `bytearray` from it:

```
mutable = bytearray(b"immutable")

```

This will let you modify the individual bytes through normal assignments

```
mutable[0] = mutable[1] = 32

```

If you need to do bit-wise operations then I suggest trying [bitstring](http://python-bitstring.googlecode.com) (with apologies for recommending my own module). It works for Python 3 and lets you do bit-wise slicing, shifting, logical operations and much more.

```
>>> s = bitstring.BitArray(bytes=b'your_bytes_object')
>>> s.hex
'0x796f75725f62797465735f6f626a656374'
>>> ten_bits = s[5:15]
>>> print(ten_bits, ten_bits.int)
0b0010110111 183
>>> print(ten_bits << 2)
0b1011011100
>>> print(s[0:6] & '0b110100')
0b010100

```
If you're using Python 2.x, you could try using [Construct](http://construct.wikispaces.com/). It can do very elegant parsing of data, including [bit data](http://construct.wikispaces.com/tut-bitwise).

It hasn't had so much active development recently, so I'm not sure what would be involved in making it work for Python 3.x. But for 2.x it's great.