Hi Can you suggest me a better way to write this code?

#include <bits/stdc++.h>
using namespace std;
int powerof2(int n)
{
int res = 0;
for (int i = n; i >= 1; i–)
{
if ((i & (i - 1)) == 0)
{
res = i;
break;
}
}
return res;
}
int main()
{

int t;
cin >> t;
while (t--)
{
	int n;
	cin >> n;
	int count = 0;
	while (n)
	{

		n = n - powerof2(n);
		count++;
	}
	cout << count << endl;


}

}

Hey @suhaib0900 just count the number of set bits in n, that will be the answer of minimum jumps that hulk would required to go at n level.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.