[erlang-questions] Bit reversing

Shimon Cohen <>
Wed Aug 22 22:12:13 CEST 2007


Hi

As reading just now Chap. 5 in "Programming Erlang",
I can't ressist the chalange. Although not very staright forward...

reverse_binary(B) ->
    list_to_binary(lists:reverse([reverse_byte(X) || X <-
binary_to_list(B)])).

reverse_byte(B) ->
    <<B0:1, B1:1, B2:1, B3:1, B4:1, B5:1, B6:1, B7:1>> = <<B>>,
    <<B7:1, B6:1, B5:1, B4:1, B3:1, B2:1, B1:1, B0:1>>.


Shimon.

On 8/22/07, Eranga Udesh <> wrote:
>
> Hi,
>
> Is there a straight forward and efficient method to reverse the bits in a
> whole binary? For eg.
>
> <<bit 1 .... bit n>>
> After reversing,
> <<bit n .... bit 1>>
>
> Thanks,
> - Eranga
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070822/05ca62cf/attachment.html>


More information about the erlang-questions mailing list