Leetcode---剑指Offer题15---二进制中1的个数

剑指Offer-面试题15---二进制中1的个数

二进制中1的个数

https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/

使用一个位运算的小技巧即可。

        public int HammingWeight(uint n)
        {
            int count = 0;

            while (n!=0)
            {
                count++;
                n = (n - 1) & n;
            }

            return count;
        }

  

原文地址:https://www.cnblogs.com/Fflyqaq/p/12886679.html