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

Magnus Klaar magnus.klaar@REDACTED
Mon Dec 26 01:57:45 CET 2011


Hi!

Why are you obsessing about creating a sub-binary in this case? A
sub-binary is _not_ a full copy of a binary, it's just a binary that's
pointing into another binary. It's not very expensive. The only time you
have any reason to go through great troubles to optimize this away is when
the compiler cannot optimize a match operation that you perform on each
iteration over a binary of unknown length.

MVH Magnius

On Sun, Dec 25, 2011 at 11:47 PM, Zabrane Mickael <zabrane3@REDACTED>wrote:

>
> On Dec 25, 2011, at 8:19 PM, Torben Hoffmann wrote:
>
> Following that you can write it like:
> set2(N, Bin) ->
> <<L:N/bits, _:1, R/bits>> = Bin, % LINE 271
>
> <<L/bits, 1:1, R/bits>>.
>
>
> on my machine (R15B, R14B04), the warning is still reported.
> src/foo.erl:271: Warning: NOT OPTIMIZED: sub binary is used or returned
>
> Regards,
> Zabrane
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111226/25e113dc/attachment.htm>


More information about the erlang-questions mailing list