Mahendra suthar
Mahendra suthar

Reputation: 469

How many numbers between 0 and n satisfy bitwise property i&y==i?

Here i is a number between 0 and n And y is any random number

//brute force
//say y is 49
count=0;
for(int i=0;i<=49;i++){
    if((i&y)==i)
        count++
}
cout<<count;

Also I would like to know what are those numbers ?

Upvotes: 0

Views: 115

Answers (1)

alain
alain

Reputation: 12047

Let b be the number of set bits in y.

Then,

2b

is the number of numbers that satisfy the property.

The numbers are those for which all bits that are not set in y are also not set.

Upvotes: 3

Related Questions