I have extracted digit by digit, flipped it using xor operator and then appended it to the result:

class Solution {

public int findComplement(int num) {

int a=0,n,i=0;

while(num>=1)

{

n=num & 1;

a+=(n ^ 1)* (int)Math.pow(2,i);

i++;

num=num>>1;

}

return a;

}

}