Skip to content

Instantly share code, notes, and snippets.

@asonge
Created February 2, 2018 03:48
Show Gist options
  • Save asonge/1e86de5f8c4a63dde4d379a209bca324 to your computer and use it in GitHub Desktop.
Save asonge/1e86de5f8c4a63dde4d379a209bca324 to your computer and use it in GitHub Desktop.
defmodule InvertImage do
@magic_reduction_number 20000
@mask :binary.copy(<<255>>, @magic_reduction_number)
def invert(binary), do: do_exor(binary, [])
defp do_exor("", acc), do: acc
defp do_exor(data, acc) when byte_size(data) < @magic_reduction_number,
do: [acc | :crypto.exor(data, :binary.copy(<<255>>, byte_size(data)))]
defp do_exor(<<data::binary-size(20000), rest::binary>>, acc),
do: do_exor(rest, [acc | :crypto.exor(data, @mask)])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment