一道二进制的考查题

来源:互联网 发布:中文科技期刊数据库 编辑:程序博客网 时间:2024/05/20 02:21

两个int32整数m和n的二进制表达,有多少个位(bit)不同么?

 public class Solution {    /**     * 获得两个整形二进制表达位数不同的数量     *      * @param m 整数m     * @param n 整数n     * @return 整型     */    public int countBitDiff(int m, int n) {        int count=0;        int v=m^n;<span style="color:#FF0000;">//异或得到结果,判断结果中1的个数即可解决问题</span>        while(v!=0){            count+=v & 1;            v>>=1;        }        return count;    }} 


0 0
原创粉丝点击