# find all integers in the list that do not share any bits except the lsb. my %MatchedIntegers=(); #my output foreach my $i1 (@LongListOfIntegers) { foreach (@LongListOfIntegers) { #if a match (only least significant bit overlaps), # add to hash of hash, I don't care about the value. $MatchedIntegers{$i1}{$_}=() if (($i1 & $_) == 1) ; } }