bits serialization clear now, directly to bytes

This commit is contained in:
protolambda 2019-07-12 22:20:07 +02:00
parent b98679957b
commit ac6d019870
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
1 changed files with 9 additions and 4 deletions

View File

@ -120,8 +120,10 @@ return b""
### `Bitvector[N]` ### `Bitvector[N]`
```python ```python
as_integer = sum([value[i] << i for i in range(len(value))]) array = [0] * ((N + 7) // 8)
return as_integer.to_bytes((N + 7) // 8, "little") for i in range(N):
array[i // 8] |= value[i] << (i % 8)
return bytes(array)
``` ```
### `Bitlist[N]` ### `Bitlist[N]`
@ -129,8 +131,11 @@ return as_integer.to_bytes((N + 7) // 8, "little")
Note that from the offset coding, the length (in bytes) of the bitlist is known. An additional leading `1` bit is added so that the length in bits will also be known. Note that from the offset coding, the length (in bytes) of the bitlist is known. An additional leading `1` bit is added so that the length in bits will also be known.
```python ```python
as_integer = (1 << len(value)) + sum([value[i] << i for i in range(len(value))]) array = [0] * ((len(value) // 8) + 1)
return as_integer.to_bytes((as_integer.bit_length() + 7) // 8, "little") for i in range(len(value)):
array[i // 8] |= value[i] << (i % 8)
array[len(value) // 8] |= 1 << (len(value) % 8)
return bytes(array)
``` ```
### Vectors, containers, lists, unions ### Vectors, containers, lists, unions