Counting Bits
Nov 14, 2026
CSES
Get Pushkar Takale’s stories in your inbox
Join Medium for free to get updates from this writer.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
ll n;
cin>>n;
ll ans=0;
n++;
for(ll i=0; i<=60; i++)
{
ll tp = (1LL<<i);
ll pt = ((n/tp)/2LL) *tp;
ans += pt;
ll tmp = n/tp;
if(tmp%2)
{
// 1 aadha cover hai
ll tmp1 = n%tp ;
ans += tmp1;
}
else
{
// zero pe end
}
}
cout<<ans<<"\n";
}



