Efficiently determine whether two arbitrarily-sized integers share a digit
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.