text: Count set bits in an integer | C # implementation for recursive approach to find the number of set bits using Brian Kernighan Algorithm ; recursive function to count set bits ; base case ; Driver Code ; get value from user ; function calling
code: using System ; class GFG { public static int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return 1 + countSetBits ( n & ( n - 1 ) ) ; } static public void Main ( ) { int n = 9 ; Console . WriteLine ( countSetBits ( n ) ) ; } }