191. Number of 1 Bits(Binary)

 1 class Solution {
 2     // you need to treat n as an unsigned value
 3     public int hammingWeight(int n) {
 4         int res = 0;
 5         for(int i = 0; i < 32; i++) {
 6             if((n & 1) != 0) {
 7                 res++;
 8             }
 9             n>>>=1;    
10         }
11         return res;
12     }
13 }

 

posted @ 2018-07-31 23:32  jasoncool1  阅读(124)  评论(0编辑  收藏  举报