public class A { public static void main(String[] args) { int cnt = 0; int a = 1200, b = 2400; while ((a ^ b) != 0) { cnt += (a % 2) ^ (b % 2); a = a >> 1; b = b >> 1; } System.out.println(cnt); } }
public class Bitwise { public static void main(String[] args) { int a = diff(123, 12345); System.out.println(a);
int b = diff(1233143142314123L, 123341243132445L); System.out.println(b); } public static int diff(int a, int b) { return countBit(a ^ b); }
public static int diff(long a, long b) { return countBit(a ^ b); } /** * 计算一个二进制数位中一共有多少个“1”,直接使用 Henry S. Warren, Hacker's Delight 一书 Figure 5-2 Counting 1-bits in a word. * 所提供的方法,原书中为 unsigned 使用 >> 运算,但在 Java 中没有 unsigned 数,因此必须使用 * 无符号移位 >>> * * @param x * @return */ private static int countBit(int x) { x = x - ((x >>> 1) & 0x55555555); x = (x & 0x33333333) + ((x >>> 2) & 0x33333333); x = (x + (x >>> 4)) & 0x0f0f0f0f; x = x + (x >>> 8); x = x + (x >>> 16); return x & 0x3f; // 如果使用 1f 时,1f 为 31,如果一个 int 全为 1 的话,使用 1f 将统计不完全,long 时同理 }
private static int countBit(long x) { x = x - ((x >>> 1) & 0x5555555555555555L); x = (x & 0x3333333333333333L) + ((x >>> 2) & 0x3333333333333333L); x = (x + (x >>> 4)) & 0x0f0f0f0f0f0f0f0fL; x = x + (x >>> 8); x = x + (x >>> 16); x = x + (x >>> 32); return (int)(x & 0x7fL); // 7f 为 127 } }
public class A {
public static void main(String[] args) {
int cnt = 0;
int a = 1200, b = 2400;
while ((a ^ b) != 0) {
cnt += (a % 2) ^ (b % 2);
a = a >> 1; b = b >> 1;
}
System.out.println(cnt);
}
}
int a = diff(123, 12345);
System.out.println(a);
int b = diff(1233143142314123L, 123341243132445L);
System.out.println(b);
} public static int diff(int a, int b) {
return countBit(a ^ b);
}
public static int diff(long a, long b) {
return countBit(a ^ b);
} /**
* 计算一个二进制数位中一共有多少个“1”,直接使用 Henry S. Warren, Hacker's Delight 一书 Figure 5-2 Counting 1-bits in a word.
* 所提供的方法,原书中为 unsigned 使用 >> 运算,但在 Java 中没有 unsigned 数,因此必须使用
* 无符号移位 >>>
*
* @param x
* @return
*/
private static int countBit(int x) {
x = x - ((x >>> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
x = (x + (x >>> 4)) & 0x0f0f0f0f;
x = x + (x >>> 8);
x = x + (x >>> 16);
return x & 0x3f; // 如果使用 1f 时,1f 为 31,如果一个 int 全为 1 的话,使用 1f 将统计不完全,long 时同理
}
private static int countBit(long x) {
x = x - ((x >>> 1) & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + ((x >>> 2) & 0x3333333333333333L);
x = (x + (x >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
x = x + (x >>> 8);
x = x + (x >>> 16);
x = x + (x >>> 32);
return (int)(x & 0x7fL); // 7f 为 127
}
}
return Integer.bitCount(a^b);
}int bitCompare(long a,long b){
return Long.bitCount(a^b);
}
int a = diff(123, 12345);
System.out.println(a);
int b = diff(1233143142314123L, 123341243132445L);
System.out.println(b);
} public static int diff(int a, int b) {
return countBit(a ^ b);
}
public static int diff(long a, long b) {
return countBit(a ^ b);
} /**
* 计算一个二进制数位中一共有多少个“1”,直接使用 Henry S. Warren, Hacker's Delight 一书 Figure 5-2 Counting 1-bits in a word.
* 所提供的方法,原书中为 unsigned 使用 >> 运算,但在 Java 中没有 unsigned 数,因此必须使用
* 无符号移位 >>>
*
* @param x
* @return
*/
private static int countBit(int x) {
x = x - ((x >>> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
x = (x + (x >>> 4)) & 0x0f0f0f0f;
x = x + (x >>> 8);
x = x + (x >>> 16);
return x & 0x3f; // 如果使用 1f 时,1f 为 31,如果一个 int 全为 1 的话,使用 1f 将统计不完全,long 时同理
}
private static int countBit(long x) {
x = x - ((x >>> 1) & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + ((x >>> 2) & 0x3333333333333333L);
x = (x + (x >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
x = x + (x >>> 8);
x = x + (x >>> 16);
x = x + (x >>> 32);
return (int)(x & 0x7fL); // 7f 为 127
}
}这种效率最高。
int countB(int a,int b){
int c=!((a|b)|(a&b));//异或
int count=0;
while(c!=0){
if(c<0){
count++;
}
c<<=1;//左移
}
}
//时间效率为T(n),n为总位数