{"id":8228,"date":"2021-06-07T18:14:10","date_gmt":"2021-06-07T12:44:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=8228"},"modified":"2021-11-22T18:40:41","modified_gmt":"2021-11-22T13:10:41","slug":"brian-kernighans-algorithm-to-count-set-bits-in-an-integer","status":"publish","type":"post","link":"https:\/\/python-programs.com\/brian-kernighans-algorithm-to-count-set-bits-in-an-integer\/","title":{"rendered":"Brian Kernighan\u2019s Algorithm to count set bits in an integer in C++ and Python"},"content":{"rendered":"
Brian Kernighan\u2019s Algorithm to count the number of set bits in an integer:<\/strong>\u00a0Given a number, the task is to count the set bits of the given number using Brian Kernighan\u2019s Algorithm in C++ and Python.<\/p>\n We\u2019ll look at Brian Kernighan\u2019s Algorithm and see how it works in C++ and Python.<\/p>\n Drive into\u00a0Python Programming Examples<\/a>\u00a0and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n First, we implement the brute force solution for the above program<\/p>\n A simple method is to take each bit into consideration in a number (set or unset) and hold a counter to track the set bits.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/b><\/p>\n Output:<\/strong><\/p>\n The brute-force strategy described above requires one repetition per bit until no more set bits remain. So it goes through 32 iterations on a 32\u2013bit word with only the high-bit set.<\/p>\n We may apply Brian Kernighan\u2019s technique to improve the performance of the naive algorithm described above. The concept is to only consider an integer\u2019s set bits by turning off its rightmost set bit (after counting it) so that the next iteration of the loop only considers the next rightmost bit.<\/p>\n To turn off the rightmost set bit of a number n, use the formula n & (n-1). This is because the formula n-1 flips all the bits following the rightmost set bit of n, including the rightmost set bit itself. As a result, n & (n-1) results in the last bit of n being flipped.<\/p>\n <\/p>\n Below is the implementation of Brian Kernighan’s Algorithm to set bits in a Python:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity:<\/strong> O(logn)<\/p>\n Output:<\/strong><\/p>\n Brian Kernighan’s algorithm iterates as many times as there are set bits. So, if we have a 32\u2013bit word with only the high bit set, it will only be looped once.<\/p>\n GCC also implements a built-in function to get no of set bits in an integer, Output:\u00a0<\/strong><\/p>\n Also, GCC furnishes two other built-in functions, Related Programs<\/strong>:<\/p>\n Brian Kernighan\u2019s Algorithm to count the number of set bits in an integer:\u00a0Given a number, the task is to count the set bits of the given number using Brian Kernighan\u2019s Algorithm in C++ and Python. Brian Kernighan\u2019s Algorithm to Count Set bits of a Number in C++ and Python We\u2019ll look at Brian Kernighan\u2019s Algorithm …<\/p>\nBrian Kernighan\u2019s Algorithm to Count Set bits of a Number in C++ and Python<\/h2>\n
\n
given number =43\r\n<\/pre>\n
The total number of set bits in the given number 43 : \r\n4\r\n<\/pre>\n
given number =64\r\n<\/pre>\n
The total number of set bits in the given number 64 : \r\n1\r\n<\/pre>\n
given number =4322\r\n<\/pre>\n
The total number of set bits in the given number 4322 : \r\n5\r\n<\/pre>\n
<\/a>Brute-Force Approach (Na\u00efve Approach)<\/h3>\n
\n
def countSetBit(numb):\r\n # checking if the given number is greater than 1\r\n if numb > 1:\r\n # Set the variable say setbitcount to 0 to count the total number of set bits.\r\n setbitcount = 0\r\n # looping till number greater than 0 using while loop\r\n while(numb > 0):\r\n # We will get the last check bit whether it is set bit or not using % operator\r\n checkbit = numb % 2\r\n # checking if the check bit is 1 or not\r\n # if the check bit is 1 then increment the setbitcount\r\n if(checkbit == 1):\r\n setbitcount = setbitcount+1\r\n # divide the number by 2\r\n numb = numb\/\/2\r\n # return the setbitcount\r\n return setbitcount\r\n\r\n\r\n# Driver code\r\ngiven_numb = 235\r\n# passing given number to countSetBit function to\r\n# count the total number of set bits in the given number\r\nprint(\"The total number of set bits in the given number \", given_numb, \" : \")\r\nprint(countSetBit(given_numb))<\/pre>\n
The total number of set bits in the given number 235 : \r\n6<\/pre>\n
<\/a>Brian Kernighan\u2019s Algorithm to calculate set bits in an integer<\/h3>\n
<\/a>Implementing Brian Kernighan\u2019s Algorithm\u00a0to count the number of set bits in an integer\u00a0<\/strong>in Python<\/h3>\n
def countSetBit(numb):\r\n # checking if the given number is greater than 1\r\n if numb > 1:\r\n # Set the variable say setbitcount to 0 to count the total number of set bits.\r\n setbitcount = 0\r\n # looping till number greater than 0 using while loop\r\n while(numb > 0):\r\n numb = numb & (numb-1)\r\n # increment the set bit count\r\n setbitcount = setbitcount+1\r\n # return the setbitcount\r\n return setbitcount\r\n\r\n\r\n# Driver code\r\ngiven_numb = 4322\r\n# passing given number to countSetBit function to\r\n# count the total number of set bits in the given number\r\nprint(\"The total number of set bits in the given number \", given_numb, \" : \")\r\nprint(countSetBit(given_numb))\r\n<\/pre>\n
The total number of set bits in the given number 4322 : 5<\/pre>\n
<\/a>Implementing Brian Kernighan\u2019s Algorithm to count the number of set bits in an integer in C++<\/h3>\n
\nBelow is the implementation of Brian Kernighan’s Algorithm to set bits in a C++:<\/strong><\/p>\n#include <iostream>\r\nusing namespace std;\r\n\/\/ function which returns the total number of set bits in\r\n\/\/ the given number\r\nint countSetBit(int numb)\r\n{ \/\/ Set the variable say setbitcount to 0 to count the\r\n \/\/ total number of set bits.\r\n int setbitcount = 0;\r\n \/\/ checking if the given number is greater than 1\r\n if (numb > 1) {\r\n \/\/ looping till number greater than 0 using while\r\n \/\/ loop\r\n while (numb > 0) { \r\n numb = numb & (numb - 1);\r\n \/\/ increment the set bit count\r\n setbitcount++;\r\n }\r\n }\r\n \/\/ return the setbitcount\r\n return setbitcount;\r\n}\r\nint main()\r\n{\r\n \/\/ given number\r\n int given_numb = 4322;\r\n \/\/ passing given number to countSetBit function to\r\n \/\/ count the total number of set bits in the given\r\n \/\/ number\r\n cout << \"The total number of set bits in the given \"\r\n \"number \"\r\n << given_numb << \" : \" << endl;\r\n cout << countSetBit(given_numb);\r\n\r\n return 0;\r\n}<\/pre>\n
The total number of set bits in the given number 4322 : \r\n5<\/pre>\n
<\/a>To Count Set Bits in an Integer Using GCC built-in function<\/h3>\n
int __builtin_popcount<\/code>(unsigned int n) that returns the total number of set bits in n. The below C++ program illustrates it clearly:<\/p>\n
#include <iostream>\r\nusing namespace std;\r\n \r\nint main()\r\n{\r\n int n = 16;\r\n cout << \"The total number of set bits in \" << n << \" is \"\r\n << __builtin_popcount (n) << endl;\r\n \r\n return 0;\r\n}<\/pre>\n
The total number of set bits in 16 is 1<\/pre>\n
int __builtin_popcountl<\/code> (unsigned long) and
int __builtin_popcountll<\/code> (unsigned long long), same as
__builtin_popcount<\/code>, except their argument type is unsigned long and unsigned long long, each.<\/p>\n
\n