We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b476a5b commit 94df093Copy full SHA for 94df093
chapter_7/exercise_5/reverse_bits.erl
@@ -0,0 +1,17 @@
1
+-module(reverse_bits).
2
+
3
+-export([reverse/1]).
4
5
+% We take a binary and then need to reverse all the bits.
6
+reverse(Binary) ->
7
+ % Call the recursive function reverse/2 with the binary and an empty binary
8
+ % as the accumlator.
9
+ reverse(Binary, <<>>).
10
11
+reverse(<<>>, Result) ->
12
+ % Reverse the binary until we get to the end, then return the accumlator
13
+ Result;
14
+reverse(<<Bit:1, Bin/bitstring>>, Result) ->
15
+ % Take the first bit off the front of the binary and push it onto the
16
+ % accumlator binary. Call the function again with the remaining bits
17
+ reverse(Bin, <<Bit:1, Result/bitstring>>).
0 commit comments