码迷,mamicode.com
首页 > 其他好文 > 详细

[剑指offer] 11. 二进制中1的个数

时间:2018-12-01 15:20:50      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:col   题目   bottom   运算   return   ott   public   div   剑指offer   

题目描述

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

利用位运算
class Solution
{
public:
  int NumberOf1(int n)
  {
    int a = 1;
    int nums = 0;
    while (a != 0)
    {
      if (n == (n | a))
        nums++;
      a <<= 1;
    }
    return nums;
  }
};

 

[剑指offer] 11. 二进制中1的个数

标签:col   题目   bottom   运算   return   ott   public   div   剑指offer   

原文地址:https://www.cnblogs.com/ruoh3kou/p/10049152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!