Number of 1 Bits
Problem Statement
Section titled “Problem Statement”Write a function that takes an unsigned integer and returns the number of 1 bits (Hamming weight).
You need to solve this problem efficiently.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
| Example 1 | Result 1 | Explanation for example 1 |
| Example 2 | Result 2 | Explanation for example 2 |
Constraints
Section titled “Constraints”- Constraint 1 for Number of 1 Bits
- Constraint 2
- Constraint 3
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Loop | O(n) | O(1) | When applicable |
| Kernighan | O(n) | O(1) | When applicable |
| Builtin | O(n) | O(1) | When applicable |
Approach 1: Loop
Section titled “Approach 1: Loop”This approach provides an efficient solution for number of 1 bits.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”def number_of_1_bits_loop(n: int) -> int: """Count 1 bits by checking each bit.""" count = 0 while n: count += n & 1 n >>= 1 return count
# Test casesprint(number_of_1_bits_loop(11)) # 3print(number_of_1_bits_loop(128)) # 1#include <cstdint>using namespace std;
int numberOf1BitsLoop(uint32_t n) { int count = 0; while (n) { count += n & 1; n >>= 1; } return count;}
int main() { cout << numberOf1BitsLoop(11) << endl; // 3 return 0;}public class NumberOf1BitsLoop { public static int numberOf1BitsLoop(int n) { int count = 0; while (n != 0) { count += n & 1; n >>>= 1; } return count; }
public static void main(String[] args) { System.out.println(numberOf1BitsLoop(11)); // 3 }}function numberOf1BitsLoop(n) { let count = 0; while (n) { count += n & 1; n >>>= 1; } return count;}
console.log(numberOf1BitsLoop(11)); // 3console.log(numberOf1BitsLoop(128)); // 1fn number_of_1_bits_loop(mut n: u32) -> i32 { let mut count = 0; while n > 0 { count += (n & 1) as i32; n >>= 1; } count}
fn main() { println!("{}", number_of_1_bits_loop(11)); // 3 println!("{}", number_of_1_bits_loop(128)); // 1}package main
import "fmt"
func numberOf1BitsLoop(num uint32) int { count := 0 for num > 0 { count += int(num & 1) num >>= 1 } return count}
func main() { fmt.Println(numberOf1BitsLoop(11)) // 3 fmt.Println(numberOf1BitsLoop(128)) // 1}Approach 2: Kernighan
Section titled “Approach 2: Kernighan”This approach provides an efficient solution for number of 1 bits.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: number-of-1-bits# Approach: kernighan
# Implementation placeholder for number_of_1_bits_kernighan# This file is auto-generated and should contain the solution code.
def number_of_1_bits_kernighan(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: number-of-1-bits# Approach: kernighan
# Implementation placeholder for number_of_1_bits_kernighan# This file is auto-generated and should contain the solution code.
def number_of_1_bits_kernighan(): pass
if __name__ == "__main__": pass# Java Solution# Problem: number-of-1-bits# Approach: kernighan
# Implementation placeholder for number_of_1_bits_kernighan# This file is auto-generated and should contain the solution code.
def number_of_1_bits_kernighan(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: number-of-1-bits# Approach: kernighan
# Implementation placeholder for number_of_1_bits_kernighan# This file is auto-generated and should contain the solution code.
def number_of_1_bits_kernighan(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: number-of-1-bits# Approach: kernighan
# Implementation placeholder for number_of_1_bits_kernighan# This file is auto-generated and should contain the solution code.
def number_of_1_bits_kernighan(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: number-of-1-bits# Approach: kernighan
# Implementation placeholder for number_of_1_bits_kernighan# This file is auto-generated and should contain the solution code.
def number_of_1_bits_kernighan(): pass
if __name__ == "__main__": passApproach 3: Builtin
Section titled “Approach 3: Builtin”This approach provides an efficient solution for number of 1 bits.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: number-of-1-bits# Approach: builtin
# Implementation placeholder for number_of_1_bits_builtin# This file is auto-generated and should contain the solution code.
def number_of_1_bits_builtin(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: number-of-1-bits# Approach: builtin
# Implementation placeholder for number_of_1_bits_builtin# This file is auto-generated and should contain the solution code.
def number_of_1_bits_builtin(): pass
if __name__ == "__main__": pass# Java Solution# Problem: number-of-1-bits# Approach: builtin
# Implementation placeholder for number_of_1_bits_builtin# This file is auto-generated and should contain the solution code.
def number_of_1_bits_builtin(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: number-of-1-bits# Approach: builtin
# Implementation placeholder for number_of_1_bits_builtin# This file is auto-generated and should contain the solution code.
def number_of_1_bits_builtin(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: number-of-1-bits# Approach: builtin
# Implementation placeholder for number_of_1_bits_builtin# This file is auto-generated and should contain the solution code.
def number_of_1_bits_builtin(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: number-of-1-bits# Approach: builtin
# Implementation placeholder for number_of_1_bits_builtin# This file is auto-generated and should contain the solution code.
def number_of_1_bits_builtin(): pass
if __name__ == "__main__": pass