diff --git a/problems/java/number-of-1-bits.java b/problems/java/number-of-1-bits.java new file mode 100644 index 0000000..57862d9 --- /dev/null +++ b/problems/java/number-of-1-bits.java @@ -0,0 +1,13 @@ +public class Solution { + public int hammingWeight(int n) { + int bits = 0; + + for (int i = 0; i < 32; ++i) { + if ((n & (1 << i)) != 0) { + ++bits; + } + } + + return bits; + } +}