bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 191. Number of 1 Bits

191. Number of 1 Bits

Leetcode Solutions

Bit Manipulation Trick

  1. Initialize a count variable to 0 to keep track of the number of '1' bits.
  2. While n is not zero: a. Perform a bitwise AND operation between n and n - 1 and store the result back in n. b. Increment the count by 1.
  3. Return the count as the Hamming weight of the number.
UML Thumbnail

Loop and Check Each Bit

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...