What does it mean ( i & (1<<j)) in second condition?

Viewed 181

C++ code. In this code, I got confused while I read the code. As you can see (i & (j<<1)) in that condition does it mean an odd number? Please let me know (i & (j<<1)) what does it mean is and how does it work?

void solve()
{
    int n, l, r, x;
    cin >> n >> l >> r >> x;
 
    int a[1000];
 
    for(int i=0; i<n; i++)
    cin >> a[i];
 
    int ans=0;
 
    for(int i=0; i< (1<<n); i++)
    {
        if(__builtin_popcount(i) >= 2)
        {
            int mx=0;
            int mn=INT_MAX;
 
            int sum=0;
 
            for(int j=0; j<n; j++)
            {
                if(i & (1<<j)) /// What does it mean ??
                {
                    sum+=a[j];
 
                    mx=max(mx, a[j]);
                    mn=min(mn, a[j]);
                }
            }
 
            if(l <= sum && sum<=r && mx-mn>=x)
            ans++;
        }
    }
 
    cout << ans << '\n';
}
1 Answers
if(i & (1<<j)) 

means "if the binary representation of i has its j'th (lowest) bit turned on".

That's because:

Caveat: n must be low enough so that (1<<j) doesn't exceed the number of bits in an int (which depends on the platform)... otherwise bad things may happen.

Related