From 8f34be49bbad31fccfad5b82e84ab59ea7ea6b10 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 29 Nov 2023 12:45:36 +0100 Subject: [PATCH] =?UTF-8?q?problems(java):=20add=20=E2=80=9C191.=20Number?= =?UTF-8?q?=20of=201=20Bits=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- problems/java/number-of-1-bits.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 problems/java/number-of-1-bits.java 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; + } +}