From 55b9080e546a5ee022bc183f831bfa2b11b7255d Mon Sep 17 00:00:00 2001 From: NISHITA97 Date: Sat, 30 Dec 2017 02:11:15 +0530 Subject: [PATCH 1/4] =?UTF-8?q?Brian=20Kernighan=E2=80=99s=20Algorithm=20a?= =?UTF-8?q?dded?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Others/BrianKernighanAlgorithm.java | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Others/BrianKernighanAlgorithm.java diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java new file mode 100644 index 00000000..361cdc05 --- /dev/null +++ b/Others/BrianKernighanAlgorithm.java @@ -0,0 +1,56 @@ +import java.util.Scanner; + +/** + * + * @author Nishita Aggarwal + * + * Brian Kernighan’s Algorithm + * algorithm to count the number of set bits in a given number + * Subtraction of 1 from a number toggles all the bits (from + * right to left) till the rightmost set bit(including the + * rightmost set bit). + * So if we subtract a number by 1 and do bitwise & with + * itself (n & (n-1)), we unset the rightmost set bit. + * If we do n & (n-1) in a loop and count the no of times loop + * executes we get the set bit count. + * Number of iterations of the loop is equal to the number of + * set bits in a given integer. + * + * Time Complexity: O(logn) + * + */ + + +public class BrianKernighanAlgorithm { + + /** + * @param num: number in which we count the set bits + * + * @return int: Number of set bits + * */ + static int countSetBits(int num) + { + int cnt = 0; + while(num != 0) + { + num = num & (num-1); + cnt++; + } + return cnt; + } + + + /** + * + * @param args : command line arguments + * + */ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + int setBitCount = countSetBits(num); + System.out.println(setBitCount); + sc.close(); + } +} From 8db4ef901c8fef6bc90d59773256c5c912f4dff9 Mon Sep 17 00:00:00 2001 From: Nishita Aggarwal Date: Sat, 30 Dec 2017 02:14:48 +0530 Subject: [PATCH 2/4] indentation improved --- Others/BrianKernighanAlgorithm.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java index 361cdc05..a4ea69ce 100644 --- a/Others/BrianKernighanAlgorithm.java +++ b/Others/BrianKernighanAlgorithm.java @@ -4,7 +4,7 @@ import java.util.Scanner; * * @author Nishita Aggarwal * - * Brian Kernighan’s Algorithm + * Brian Kernighan’s Algorithm * algorithm to count the number of set bits in a given number * Subtraction of 1 from a number toggles all the bits (from * right to left) till the rightmost set bit(including the @@ -24,10 +24,10 @@ import java.util.Scanner; public class BrianKernighanAlgorithm { /** - * @param num: number in which we count the set bits - * - * @return int: Number of set bits - * */ + * @param num: number in which we count the set bits + * + * @return int: Number of set bits + * */ static int countSetBits(int num) { int cnt = 0; From 5cf220362a37c982a26dc81f2e87159fe99b2495 Mon Sep 17 00:00:00 2001 From: Nishita Aggarwal Date: Sun, 31 Dec 2017 15:02:56 +0530 Subject: [PATCH 3/4] spacing improved --- Others/BrianKernighanAlgorithm.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java index a4ea69ce..eaf43093 100644 --- a/Others/BrianKernighanAlgorithm.java +++ b/Others/BrianKernighanAlgorithm.java @@ -5,16 +5,18 @@ import java.util.Scanner; * @author Nishita Aggarwal * * Brian Kernighan’s Algorithm + * * algorithm to count the number of set bits in a given number + * * Subtraction of 1 from a number toggles all the bits (from * right to left) till the rightmost set bit(including the * rightmost set bit). * So if we subtract a number by 1 and do bitwise & with - * itself (n & (n-1)), we unset the rightmost set bit. + * itself i.e. (n & (n-1)), we unset the rightmost set bit. + * * If we do n & (n-1) in a loop and count the no of times loop * executes we get the set bit count. - * Number of iterations of the loop is equal to the number of - * set bits in a given integer. + * * * Time Complexity: O(logn) * From 1567b58a46c2a7f7d92af4e8cb0c712d78569a08 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sun, 31 Dec 2017 06:47:51 -0800 Subject: [PATCH 4/4] Update BrianKernighanAlgorithm.java --- Others/BrianKernighanAlgorithm.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java index eaf43093..cb27eec1 100644 --- a/Others/BrianKernighanAlgorithm.java +++ b/Others/BrianKernighanAlgorithm.java @@ -8,14 +8,11 @@ import java.util.Scanner; * * algorithm to count the number of set bits in a given number * - * Subtraction of 1 from a number toggles all the bits (from - * right to left) till the rightmost set bit(including the + * Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the * rightmost set bit). - * So if we subtract a number by 1 and do bitwise & with - * itself i.e. (n & (n-1)), we unset the rightmost set bit. + * So if we subtract a number by 1 and do bitwise & with itself i.e. (n & (n-1)), we unset the rightmost set bit. * - * If we do n & (n-1) in a loop and count the no of times loop - * executes we get the set bit count. + * If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count. * * * Time Complexity: O(logn)