/*
5.6 Conversion: Write a function to determine the number of bits you would need to flip to convert
integer A to integer B.
EXAMPLE
Input: 29 (or: 11101), 15 (or: 01111) Output: 2
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println(convert(29, 15));
}
//要找不同的位置 直接异或 找到1的个数就行;
public static int convert(int a, int b) {
int count = 0;
for(int c = a ^ b; c != 0; c >>= 1) {
if((c & 1) == 1) {
count++;
}
}
return count;
}
}
最后
以上就是迅速蛋挞最近收集整理的关于Conversion java 走地牙CTCI 5.6的全部内容,更多相关Conversion内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复