Skip to content

Instantly share code, notes, and snippets.

@vinoski
Last active October 18, 2016 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinoski/02d4ae80440ca1f96c2e7ecb188a9088 to your computer and use it in GitHub Desktop.
Save vinoski/02d4ae80440ca1f96c2e7ecb188a9088 to your computer and use it in GitHub Desktop.
bitwise xor of bit lists
-module(x).
-export([lxor/2]).
%%% see http://stackoverflow.com/a/40098595/409228 for context
lxor(L1,L2) when is_list(L1), is_list(L2) ->
%% convert the input lists to binaries, then call lxor/2 on the results
lxor(list2bin(L1),list2bin(L2));
lxor(B1,B2) when is_binary(B1), is_binary(B2) ->
%% decode unsigned integers from the input binaries, then call lxor/2
%% on the results
lxor(binary:decode_unsigned(B1),binary:decode_unsigned(B2));
lxor(V1,V2) when is_integer(V1), is_integer(V2) ->
%% bitwise xor the input integers
V = V1 bxor V2,
%% convert the bxor result to a list of bits
convert(V).
list2bin(L) ->
%% reverse the list, combine the bits into a bitstring
V = << <<X:1>> || X <- lists:reverse(L) >>,
%% calculate the number of bits needed to pad the resulting bitstring
%% out to a binary
PadBits = (8 - (bit_size(V) band 7)) band 7,
%% return a binary holding the numeric value of the original bitlist
<<0:PadBits, V/bitstring>>.
convert(0) ->
[0];
convert(V) ->
%% assuming the input value fits in 64 bits, convert to a list of bits
Bits = [X || <<X:1>> <= <<V:64>>],
%% drop all leading 0 bits, then reverse the result
lists:reverse(lists:dropwhile(fun(D) -> D =:= 0 end, Bits)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment