Skip to content

Instantly share code, notes, and snippets.

@kevsmith
Created August 11, 2020 14:28
Show Gist options
  • Save kevsmith/1dd533e01d6baef91b974585663c3a1f to your computer and use it in GitHub Desktop.
Save kevsmith/1dd533e01d6baef91b974585663c3a1f to your computer and use it in GitHub Desktop.
defmodule Addr do
def extract_subnet(addr, mask) when is_binary(addr) and is_binary(mask) do
<<converted_addr::integer-big-size(32)>> = addr
<<converted_mask::integer-big-size(32)>> = mask
subnet = Bitwise.band(converted_addr, converted_mask)
<<octet1::integer-big-size(8), octet2::integer-big-size(8), octet3::integer-big-size(8),
octet4::integer-big-size(8)>> = <<subnet::integer-big-size(32)>>
{octet1, octet2, octet3, octet4}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment