“Bitwise Count Total Set Bits” Réponses codées

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

paramjeetdhiman

comptage de bits

countBits = (n) => n.toString(2).split("0").join("").length;
Fylls

Réponses similaires à “Bitwise Count Total Set Bits”

Questions similaires à “Bitwise Count Total Set Bits”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code