`

Number of 1 Bits——Bit Manipulation

 
阅读更多

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.

class Solution(object):
    def hammingWeight(self, n):
        """
        :type n: int
        :rtype: int
        """
        re = 0
        while n:
        	n = n & (n-1)
        	re += 1
        return re 

 

分享到:
评论

相关推荐

    python实现位操作 Bit Manipulation 课程设计

    python实现: 二进制和运算符 二进制计数设置位 二进制计数尾随零 二进制或运算符 二进制移位 二进制二进制补码 二进制异或运算符 ... Count Number Of One Bits Gray Code Sequence Highest Set Bit

    Matters Computational-ideas, algorithms, source code

    - **Counting the Bits and Blocks of a Word**: Methods for counting the number of set bits and blocks in a word. - **Words as Bitsets**: Treating words as sets of bits for efficient data ...

    80x86指令-2进制码对照表

    Date Clock Speeds Bus Width Number of Transistors Addressable Memory Virtual Memory Brief Description 4004 11/15/71 108 KHz 4 bits 2,300 (10 microns) 640 bytes First microcomputer chip, Arithmetic ...

    Matters Computational ideas, algorithms, source code

    This topic discusses algorithms for counting the number of set bits or blocks within a word. These are fundamental operations in many computing tasks. #### 1.9 Words as Bitsets Here, the concept of ...

    计算机组成与结构体系英文课件:Chapter9 Arithmetic

    An unsigned integer can range from 0 to (2^n) - 1, where 'n' is the number of bits used for the representation. For example, an 8-bit unsigned integer can represent values from 0 to 255. **2. ...

    HIME 系列加密lib

    - Huge integer number mathematical, bit manipulation and boolean functions can serve as building blocks for implementing other public key encryption schemes or performing math calculations with ...

    强大的免费的十六进制编辑器

    - Bit manipulation (view or set bits) - Open file in Read Only mode (e.g. if opened by another application or to avoid unintentional modifications) - Insert file contents into file - Write block to ...

    LeetCode最全代码

    191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 201 | [Bitwise AND of ...

    eac3to V3.17

    * fixed: number of subtitles was not appended to demuxed subtitles' file name * fixed: dialnorm removal (for Nero decoder) failed with some 2.0 TrueHD files v3.16 * added undocumented "-no2ndpass" ...

    The Art of Assembly Language Programming

    Boolean Algebra 2.0 - Chapter Overview 2.1 - Boolean Algebra 2.2 - Boolean Functions and Truth Tables 2.3 - Algebraic Manipulation of Boolean Expressions 2.4 - Canonical Forms 2.5 -...

    LeetCode练习答案

    - **计算1的位数(Number of 1 Bits)**: 计算一个无符号整数的二进制表示中有多少个1位。 ##### 树(Tree) - **二叉树的深度(Depth of Binary Tree)**: 计算一棵二叉树的深度。 - **构建二叉树(Construct Binary ...

    Leetcode题目+解析+思路+答案.pdf

    - **Number of 1 Bits**:计算一个整数中1的个数。 3. **树(Tree)**: - **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:根据给定的数组构建二叉树。 - **Binary Tree Level ...

    算法-leetcode-剑指offer上的题很多

    - **数学与位操作(Math and Bit Manipulation)**: 利用数学运算和位运算来解决特定问题。 - **单一数字问题(Single Number)**: 找出数组中唯一的不重复的数字。 - **快乐数(Happy Number)**: 判断一个数字是否快乐。...

    Java 自学宝典 第二章 数据类型

    ##### 2.6.2 The number of bits used to represent an int value in two's complement binary form. `int` 类型使用32位来表示整数值,采用补码形式。 ##### 2.6.3 Bit manipulation methods in Integer: ...

Global site tag (gtag.js) - Google Analytics