Horje
bitwise count total set bits Code Example
bit counting
countBits = (n) => n.toString(2).split("0").join("").length;
bitwise count total set bits
//WAP to find setbits (total 1's in binary ex. n= 5 => 101 => 2 setbits
int count{}, num{};
  cin >> num;

  while (num > 0) {
    count = count + (num & 1); // num&1 => it gives either 0 or 1
    num = num >> 1;	// bitwise rightshift 
  }

	 cout << count; //count is our total setbits





Cpp

Related
how to append to a vector c++ Code Example how to append to a vector c++ Code Example
c++ natural log Code Example c++ natural log Code Example
height of the tree Code Example height of the tree Code Example
Counting Sort C++ Code Example Counting Sort C++ Code Example
c++ find minimum value in vector Code Example c++ find minimum value in vector Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
11