Created
July 31, 2019 02:28
-
-
Save tony612/9b046a4a9b4b4e44935b0bc44fbada17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For binary 0000 0001 0100 0000 1000 1000 | |
# The result is 0b1 + 0b100_0000 + 0b1000 = 73 | |
defmodule BinaryParseFast do | |
def parse(bin) do | |
parse(bin, 0) | |
end | |
# A byte beginning with 0 means the end, but still need to add the last 7 bits | |
def parse(<<0::1, x::7, _::bits>>, acc), do: acc + x | |
# A byte beginning with 1 means there is more data needed to be processed. | |
# and we need to add the last 7 bits | |
def parse(<<1::1, x::7, rest::bits>>, acc) do | |
parse(rest, acc + x) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment