Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/leetcode/easy/NumberOf1Bits191.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Tags: Bit Manipulation, Divide and Conquer
package com.leetcode.easy;

public class NumberOf1Bits191 {
/**
* Time complexity: O(1)
* Space complexity: O(1)
*/
public int hammingWeight(int n) {
int res = 0;
while (n != 0) {
if (n % 2 == 1) {
res++;
}
n = n >> 1;
Comment on lines +12 to +15
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hammingWeight uses n % 2 == 1 and an arithmetic right shift (n >> 1). For negative inputs (which occur when interpreting the LeetCode "unsigned 32-bit" value as a Java int), n % 2 is negative and n >> 1 preserves the sign bit, so n may never reach 0 (e.g., n = -1 loops forever) and the bit count is incorrect. Use bit operations that work for all 32 bits, e.g. check (n & 1) != 0 and shift with n >>>= 1, or use the common n &= (n - 1) loop.

Suggested change
if (n % 2 == 1) {
res++;
}
n = n >> 1;
if ((n & 1) != 0) {
res++;
}
n >>>= 1;

Copilot uses AI. Check for mistakes.
}
return res;
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/leetcode/easy/NumberOf1Bits191Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.leetcode.easy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class NumberOf1Bits191Test {
private NumberOf1Bits191 solution;

@BeforeEach
void setUp() {
solution = new NumberOf1Bits191();
}

@Test
void testHammingWeight_Example1() {
int n = 11; // Binary representation: 1011
int expected = 3;
int result = solution.hammingWeight(n);
assertEquals(expected, result);
Comment on lines +16 to +21
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests only cover positive int inputs, but this problem treats the argument as an unsigned 32-bit value; in Java that includes negative ints (e.g. n = -1 should return 32). Adding a negative-input test (and optionally n = 0) would match the problem contract and catch issues with signed shifting/modulus in the implementation.

Copilot uses AI. Check for mistakes.
}

@Test
void testHammingWeight_Example2() {
int n = 128; // Binary representation: 10000000
int expected = 1;
int result = solution.hammingWeight(n);
assertEquals(expected, result);
}

@Test
void testHammingWeight_Example3() {
int n = 2147483645; // Binary representation: 1111111111111111111111111111101
int expected = 30;
int result = solution.hammingWeight(n);
assertEquals(expected, result);
}
}