Count Total Set Bits In All Numbers From 1 To N Code Example


Example: 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

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable