我是靠谱客的博主 迅速蛋挞,这篇文章主要介绍Conversion java 走地牙CTCI 5.6,现在分享给大家,希望可以做个参考。

/*
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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部