《剑指offer》二进制中1的个数

一、题目描述

输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

二、牛客网提供的框架

class Solution {
public:
     int  NumberOf1(int n) {

     }
};

三、代码

class Solution {
public:
     int  NumberOf1(int n) {
         int oneCount;
         oneCount = 0;
         for(int i = 0; i < 32; i++)
         {
             if(n & 1) oneCount++;
             n = n>>1;
         }

         return oneCount;
     }
};
posted @ 2016-06-28 15:06  chenximcm  阅读(86)  评论(0编辑  收藏  举报