【2】求两个整数的二进制表达式中不同的位数

 1 package com.oj.test;
 2 
 3 /**
 4  * 题目描述
 5     世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么? 
 6  * @author WGS
 7  */
 8 public class Exe1 {
 9 
10     public int countBitDiff(int m, int n) {
11         if(m==0 && n==0)
12             return 0;
13         int num = m ^ n;
14         int count=0;
15         int flag = 1;
16         while(flag != 0){
17             if((num & flag)!=0 )
18                 count++;
19             
20             flag=flag<<1;
21         }
22         return count;
23     }
24     public static void main(String[] args) {
25         Exe1 e=new Exe1();
26         System.out.println(e.countBitDiff(1999,2299));
27     }
28 
29 }

 

转载于:https://www.cnblogs.com/noaman/p/5904988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值