public int compare(String binary1,String binary2){
char[] ch1 = binary1.toCharArray();
char[] ch2 = binary2.toCharArray();
int tmp = 0;
for(int i=0;i<ch1.length;i++){
if(ch1[i]!=ch2[i]) tmp++;
}
return tmp;
}
解决方案 »
- 同一目录下两个.java程序如何编译运行?
- 大家来挑挑错。说出合适理由的都有分。
- Calendar 与 GregorianCalendar 有什么区别? 编程中我用哪个?
- java 中实现象象vb中的doevents功能怎么解决(在线等待)
- 在Java中,怎样把一个图片文件写入到mssql数据库中,怎样读出来显示?
- 请教
- 求助!怎样让JB9不再自动运行JB0SS???
- 关于CallalbleStatement的问题
- 怎样是编好的java application在windows上像windows应用程序一样直接运行
- 请教高手一个Java Socket的入门级的小问题。
- 虚方法调用与可变参数问题
- 代码中需要两个结果集要怎么写?
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为总位数