Skip to content

Instantly share code, notes, and snippets.

@keymone keymone/gist:19393
Created Oct 24, 2008

Embed
What would you like to do?
% list of {type, flag} tuples
pack(List) -> pack(List, <<>>).
pack([], Ready) -> Ready;
pack([{Type, Flag}|Rest], Ready) ->
Bit = ?MODULE:Type(Flag) - 1,
Size = size(Ready) * 8,
io:format("switch bit ~p in ~p bits~n", [Bit, Size]),
switch(Bit, Size, Rest, Ready).
switch(Bit, Size, Rest, Ready) when Bit >= Size ->
BB = (Bit - Size) rem 8,
Zeros = ((Bit - Size) div 8) * 8,
NewReady = <<Ready/binary,
0:Zeros/integer,
(1 bsl BB):8/integer>>,
pack(Rest, NewReady);
switch(Bit, _, Rest, Ready) ->
ByteNum = max(Bit div 8, 0),
<<PreByte:ByteNum/binary,
Byte:8/integer,
PostByte/binary>> = Ready,
BB = Bit - (Bit div 8) * 8,
NewByte = Byte bor (1 bsl BB),
NewReady = <<PreByte/binary,
NewByte:8/integer,
PostByte/binary>>,
pack(Rest, NewReady).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.