From 9114177360af4e1435f9eb9df3b3852544dcd3c7 Mon Sep 17 00:00:00 2001
From: Pratyush Singh <aries.pratyush@gmail.com>
Date: Sun, 11 Sep 2022 00:55:39 +0530
Subject: [PATCH 1/2] Added file to count set bits in a number

---
 .../com/thealgorithms/others/countSetBits.java    | 12 ++++++++++++
 .../thealgorithms/others/countSetBitsTest.java    | 15 +++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 src/main/java/com/thealgorithms/others/countSetBits.java
 create mode 100644 src/test/java/com/thealgorithms/others/countSetBitsTest.java

diff --git a/src/main/java/com/thealgorithms/others/countSetBits.java b/src/main/java/com/thealgorithms/others/countSetBits.java
new file mode 100644
index 000000000000..281829ae8d91
--- /dev/null
+++ b/src/main/java/com/thealgorithms/others/countSetBits.java
@@ -0,0 +1,12 @@
+package com.thealgorithms.others;
+
+public class countSetBits {
+    public long countsetBits(long num){
+        long cnt=0;
+        while(num>0){
+            cnt++;
+            num&=(num-1);
+        }
+        return cnt;
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/com/thealgorithms/others/countSetBitsTest.java b/src/test/java/com/thealgorithms/others/countSetBitsTest.java
new file mode 100644
index 000000000000..b43c8d29e335
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/countSetBitsTest.java
@@ -0,0 +1,15 @@
+package com.thealgorithms.others;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+public class countSetBitsTest {
+    @Test
+    void testSetBits(){
+        countSetBits csb= new countSetBits();
+        assertEquals(1L,csb.countsetBits(16));
+        assertEquals(4, csb.countsetBits(15));
+        assertEquals(5, csb.countsetBits(10000));
+        assertEquals(5, csb.countsetBits(31));
+    }
+}
\ No newline at end of file

From 6dce4f7a81283b41ee85660402b35d3d1732b659 Mon Sep 17 00:00:00 2001
From: Pratyush Singh <aries.pratyush@gmail.com>
Date: Sun, 11 Sep 2022 01:00:18 +0530
Subject: [PATCH 2/2] added file to count set bits of a number in its bin
 equivalent

---
 .../thealgorithms/others/countSetBits.java    | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/src/main/java/com/thealgorithms/others/countSetBits.java b/src/main/java/com/thealgorithms/others/countSetBits.java
index 281829ae8d91..33be26fd2b05 100644
--- a/src/main/java/com/thealgorithms/others/countSetBits.java
+++ b/src/main/java/com/thealgorithms/others/countSetBits.java
@@ -1,6 +1,40 @@
 package com.thealgorithms.others;
 
 public class countSetBits {
+    /** 
+     * The below algorithm is called as Brian Kernighan's algorithm 
+     * We can use Brian Kernighan’s algorithm to improve the above naive algorithm’s performance. The idea is to only consider the set bits of an integer by turning off its rightmost set bit (after counting it), so the next iteration of the loop considers the next rightmost bit.
+
+        The expression n & (n-1) can be used to turn off the rightmost set bit of a number n. This works as the expression n-1 flips all the bits after the rightmost set bit of n, including the rightmost set bit itself. Therefore, n & (n-1) results in the last bit flipped of n.
+
+        For example, consider number 52, which is 00110100 in binary, and has a total 3 bits set.
+
+        1st iteration of the loop: n = 52
+        
+        00110100    &               (n)
+        00110011                    (n-1)
+        ~~~~~~~~
+        00110000
+        
+        
+        2nd iteration of the loop: n = 48
+        
+        00110000    &               (n)
+        00101111                    (n-1)
+        ~~~~~~~~
+        00100000
+        
+        
+        3rd iteration of the loop: n = 32
+        
+        00100000    &               (n)
+        00011111                    (n-1)
+        ~~~~~~~~
+        00000000                    (n = 0)
+        
+     * @param num takes Long number whose number of set bit is to be found
+     * @return the count of set bits in the binary equivalent
+    */
     public long countsetBits(long num){
         long cnt=0;
         while(num>0){