### How to combine bytearrays fast ?

Posted:

**Wed Feb 12, 2020 9:30 pm**Hi folks,

I'm a little stuck here and maybe someone can help.

There are four bytearrays which have to be combined (for lack of a better word) into a fifth one using the fastest way possible.
The result should look like this:
The easiest way would be:
Unfortunately the operation has to be as fast as possible.

Something like
would be nice, but of course it doesn't work that way.

Has someone perhaps a clever idea how to do this as fast as possible?

Thanks a lot in advance

Blechi

I'm a little stuck here and maybe someone can help.

There are four bytearrays which have to be combined (for lack of a better word) into a fifth one using the fastest way possible.

Code: Select all

```
a = bytearray(240)
b = bytearray(240)
c = bytearray(240)
d = bytearray(240)
result = bytearray(240)
a = [<arbitrary byte>, 0, 0, 0, <arbitrary byte>, 0, 0, 0, ... and so on ..., <arbitrary byte>, 0, 0, 0]
b = [0, <arbitrary byte>, 0, 0, 0, <arbitrary byte>, 0, 0, ... and so on ..., 0, <arbitrary byte>, 0, 0]
c = [0, 0, <arbitrary byte>, 0, 0, 0, <arbitrary byte>, 0, ... and so on ..., 0, 0, <arbitrary byte>, 0]
d = [0, 0, 0, <arbitrary byte>, 0, 0, 0, <arbitrary byte>, ... and so on ..., 0, 0, 0, <arbitrary byte>]
```

Code: Select all

`result = [<byte 0 from a>, <byte 1 from b>, <byte 2 from c>, <byte 3 from d>, <byte 4 from a>, ... and so on ..., <byte 236 from a>, <byte 237 from b>, <byte 238 from c>, <byte 239 from d>]`

Code: Select all

```
for i in range(0,240):
result[i] = a[i] + b[i] + c[i] + d[i]
```

Something like

Code: Select all

`result = a | b | c | d`

Has someone perhaps a clever idea how to do this as fast as possible?

Thanks a lot in advance

Blechi