3

I'm trying to reverse binary like this:

reverse(Bin) ->
    list_to_binary(lists:reverse([rev_bits(<<B>>) || B <- binary:bin_to_list(Bin)])).


rev_bits(<<A:1, B:1, C:1, D:1, E:1, F:1, G:1, H:1>>) ->
    <<H:1, G:1, F:1, E:1, D:1, C:1, B:1, A:1>>.

I don't like this code. Could you please advise better way to accomplish this routine?

4
  • What don't you like about it, what would "better" mean to you, and why do you want to reverse the bits of a number? Dec 29, 2013 at 23:01
  • This version does not work if the binary has not a length multiple of byte.
    – Pascal
    Dec 30, 2013 at 7:55
  • @NathanielWaisbrot I just dislike smell of my code, it is just an exercise.
    – taro
    Dec 30, 2013 at 9:32
  • @Pascal I see, binary is enough, no need to be compatible with bitstrings here.
    – taro
    Dec 30, 2013 at 9:33

3 Answers 3

8

Somewhat like your rev_bits function:

rev (<<>>, Acc) -> Acc;
rev (<<H:1/binary, Rest/binary>>, Acc) ->
    rev(Rest, <<H/binary, Acc/binary>>).

I believe binary concatenation is optimised so this should be quite fast already.

Edited: use clauses instead of case…of…end.

2
  • Thank you! Recursive solution seems better here, but I rather split it into two clauses.
    – taro
    Dec 30, 2013 at 9:35
  • 1
    This will start working with bitstrings if replace every "binary" with "bits".
    – taro
    Dec 30, 2013 at 19:12
8

Better alternative:

rev(Binary) ->

   Size = erlang:size(Binary)*8,
   <<X:Size/integer-little>> = Binary,
   <<X:Size/integer-big>>.

Benchmark results of comparing to fenollp iteration method. The benchmark test was done calling both functions with a random binary containing 8192 random bytes:

Calling reverse 10 times

BENCHMARK my method: Calling reverse/1 function 10 times. Process took 0.000299 seconds BENCHMARK fenollp iteration method: Calling reverse_recursive/1 function 10 times. Process took 0.058528 seconds

Calling reverse 100 times

BENCHMARK my method: Calling reverse/1 function 100 times. Process took 0.002703 seconds BENCHMARK fenollp iteration method: Calling reverse_recursive/1 function 100 times. Process took 0.391098 seconds

The method proposed by me is usually at least 100 times faster.

2
  • 1
    Instead of erlang:size(Binary)*8, you can just use erlang:bit_size(Binary)?
    – brucify
    Jan 29, 2021 at 15:26
  • @brucify You are quite right to ask this question. I somehow got the idea that we are dealing with reversing bitstrings binaries that contains bytes only. Let's remember that for bitstrings, the number of whole bytes is returned by the erlang:size. But if if the number of bits in the binary is not divisible by 8, the resulting number of bytes is rounded down. So for an arbitrary bitstring containing 13 bit for example my erlang:size*8 will return 8 when in fact there are a total of 13 bits to be reversed and matched. I will modify my answer to include this observation. Thank's! Aug 3, 2022 at 20:07
1
binary:encode_unsigned(binary:decode_unsigned(Bin, little)).
1
  • 2
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – Isma
    Dec 13, 2017 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.