Skip to content

Instantly share code, notes, and snippets.

@tony612
Created July 31, 2019 02:28
Show Gist options
  • Save tony612/9b046a4a9b4b4e44935b0bc44fbada17 to your computer and use it in GitHub Desktop.
Save tony612/9b046a4a9b4b4e44935b0bc44fbada17 to your computer and use it in GitHub Desktop.
# 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