Efficiently determine whether two arbitrarily-sized integers share a digit My current algorithm creates two new integers initialized to 0 (one for each input), then sets the nth bit to 1 if the integer's corresponding input contains the digit n. The bitwise AND of these integers is then 0 if the inputs don't share a digit. 

I'm pretty sure there's a way to make this function more efficient. Is there something I'm missing? Example implementation (in Java) below.

boolean shareDigits(long a, long b)
{
    int aBits=0, bBits=0;
    if(a==0) aBits=1;
    if(b==0) bBits=1;
    while(a!=0)
    {
        aBits |= (1 &lt;&lt; (a % 10));
        a/=10;
    }
    while(b!=0)
    {
        bBits |= (1 &lt;&lt; (b % 10));
        b/=10;
    }
    return (aBits &amp; bBits) &gt; 0;
}
Edge cases

You don't handle negative integers very well. Specifically, the modulo operator, when performed on a negative integer with a positive divisor, may return a negative value. And when this negative value is something like, say, -2, then you get 1 &lt;&lt; -2, which results in setting a high bit instead of the bits for 0-9.

Duplication

You currently have duplicated the code for a and b. By making use of smaller methods, you can get rid of the duplication:

int getBitsFor(long num) 
{
    int bits=0;
    if(num==0) bits=1;
    while(num!=0)
    {
        bits |= (1 &lt;&lt; (num % 10));
        num/=10;
    }
    return bits;
}


And then use like so:

boolean shareDigits(long a, long b)
{
    return (getBitsFor(a) &amp; getBitsFor(b)) &gt; 0;
}


Readability

It would help if you were to put spaces between operators and the operands. Basically, I find int aBits = 0 more readable than int aBits=0. The less effort I have to spend on interpreting the code, the more effort I can spend actually writing or improving the code.

For reference:

int getBitsFor(long num) 
{
    int bits = 0;
    if(num == 0) bits = 1;
    while(num != 0)
    {
        bits |= (1 &lt;&lt; (num % 10));
        num /= 10;
    }
    return bits;
}


Compare with the snippet above, see which feels easier to read for you.

Simplification

200_success pointed this out, but they deleted their answer (although I found that it made a good point):

If you wrote do-while loops instead, then you would not need the special case for num == 0:

int getBitsFor(long num) 
{
    int bits = 0;
    do
    {
        bits |= (1 &lt;&lt; (num % 10));
        num /= 10;
    } while (num != 0)
    return bits;
}


Performance

You want to know if there's a faster way check if integers share a digit.

To be honest, I don't know. Your implementation looks pretty fast. But your implementation also takes a constant amount of calculations related to the size of the input and makes no use of shortcuts.

One shortcut that I can come up with - if a number contains all the digits, then you don't need to continue.

But perhaps checking for this case makes the worst case slower again...

I also checked https://graphics.stanford.edu/~seander/bithacks.html which is a collection of "Bit Twiddling Hacks", but there doesn't seem to be an algorithm for checking for duplicated digits in there.