概述
/* 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 java 走地牙CTCI 5.6所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复