[erlang-questions] How to efficiently set the bit at position Index to 1 in a binary?

Zabrane Mickael zabrane3@REDACTED
Sat Dec 24 19:37:36 CET 2011


Hi folks,

Here's my code:

%%--------------------------------------------------------------------
%% @doc Set the bit at position Index to 1 in the binary Bin.
%% @end
%%--------------------------------------------------------------------
-spec set(Bin :: binary(), Index :: non_neg_integer()) -> binary().
set(<<1:1, _/bits>> = Bin, 0) ->
    Bin;
set(<<0:1, Rest/bits>>, 0) ->
    <<1:1, Rest/bits>>;
set(Bin, Index) when Index > 0 ->
    Before = Index - 1,
    <<Head:Before, _:1, Rest/bits>> = Bin,
    <<Head:Before, 1:1, Rest/bits>>.


After compiling, I'm getting the following warnings:
src/foo.erl:248: Warning: INFO: the '=' operator will prevent delayed sub binary optimization
src/foo.erl:248: Warning: INFO: the '=' operator will prevent delayed sub binary optimization
src/foo.erl:250: Warning: NOT OPTIMIZED: sub binary is used or returned
src/foo.erl:254: Warning: NOT OPTIMIZED: sub binary is used or returned

What's the best/optimized solution and how to get rid of these warnings?

Regards,
Zabrane

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111224/76a05d5b/attachment.htm>


More information about the erlang-questions mailing list