noaman_wgs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 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 }

 

posted on 2016-09-25 09:49  noaman_wgs  阅读(568)  评论(0编辑  收藏  举报