Wolfram Research

Function Repository Resource:

BitListToByteArray

Source Notebook

Convert a list of bits to a byte array

Contributed by: Arnoud Buzing

ResourceFunction["BitListToByteArray"][bits]

converts the list bits to a ByteArray.

Details and Options

The output typically requires 64 times less memory than the input.

Examples

Basic Examples

Create a list of bits:

In[1]:=
bits = RandomInteger[1, 160]
Out[1]=

Convert to a byte array:

In[2]:=
ba = ResourceFunction["BitListToByteArray"][bits]
Out[2]=

Compare the number of bytes needed to represent the expressions:

In[3]:=
ByteCount /@ {bits, ba}
Out[3]=

For larger bit arrays, the relative memory compression does approach a factor of 64:

In[4]:=
bits = RandomInteger[1, 16000];
ba = ResourceFunction["BitListToByteArray"][bits];
Divide @@ N[ByteCount /@ {bits, ba}]
Out[6]=

Resource History

License Information