[erlang-questions] Bit reversing

Eranga Udesh casper2000a@REDACTED
Thu Aug 23 04:16:29 CEST 2007


Hi,

Thanks for the code snippet. However what I wanted to find out is whether
there’s a BIF or Function which can do this without the need of binary/bit
segmentation, list creation, list reversing and recreation the reversed
binary. The complexity of this method is too high write a method that is
heavily used.  A little improvement I can suggest to your code is in
reverse_byte/1 function, we can do a byte decode in little/big endian and
repack is in opposite endian.

So, anything available like that?

Also is it possible to segment a binary starting from tail?

Eg. with bitlevel_binaries and binary_comprehension compiler flags
[ X || <<X:7>> <= <<1,2,3,4,5,6>>] ---> Results ---> [0,64,64,48,32,20]

Ie. It segments the binary starting from the beginning. In bit
representation,
00000001 00000010 00000011 00000100 00000101 00000110 ---> Results --->
0000000 1000000 1000000 0110000 0100000 0010100 000110

How do I make it to give result,
[32,32,24,16,10,6]
In bit representation,
00000001 00000010 00000011 00000100 00000101 00000110 ---> Results --->
000000 0100000 0100000 0011000 0010000 0001010 0000110

Thanks,
- Eranga



From: erlang-questions-bounces@REDACTED
[mailto:erlang-questions-bounces@REDACTED] On Behalf Of Shimon Cohen
Sent: Thursday, August 23, 2007 1:42 AM
To: erlang-questions@REDACTED
Subject: Re: [erlang-questions] Bit reversing

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 <casper2000a@REDACTED> 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
erlang-questions@REDACTED
http://www.erlang.org/mailman/listinfo/erlang-questions






More information about the erlang-questions mailing list