I want to implement a program which tests if a&b==0 . It should return false if the two integers have at least 1 bit in the same location and true if they have 1 bit in different locations.
Here is my code:
import java.util.*;
public class btest{
public static boolean testand(int a,int b){
int i=0;
int k=0;
int m=0;
while(i<32){
k= (a>>i) &1;
m=(b>>i) &1;
if (k==1 && m==1){
System.out.println("they have bit 1 in common at position:"+i);
return false;
}
i++;
}
System.out.println(" a & b is equal :" + (a &b));
return true;
}
public static void main(String[]args){
Scanner scnr=new Scanner(System.in);
int a=scnr.nextInt();
int b=scnr.nextInt();
System.out.println(testand(a,b));
}
}
It works for small values. Will it be correct for large numbers too?