here is the code and it returns "IGNORE HIM!" when the size is odd
#include <set>
std::set<char> s;
int main(){
char c;
while(std::cin>>c)s.insert(c);
std::cout<<(s.size()&1?"IGNORE HIM!":"CHAT WITH HER!");
return 0;
}
here is the code and it returns "IGNORE HIM!" when the size is odd
#include <set>
std::set<char> s;
int main(){
char c;
while(std::cin>>c)s.insert(c);
std::cout<<(s.size()&1?"IGNORE HIM!":"CHAT WITH HER!");
return 0;
}
You've already noticed the pattern, i.e. set.size()&1
is true when the size is odd.
Doing a bitwise-and (&
) on a number will set all the bits to 0, except for the last bit, if it's 1. The last bit is only 1 when the number is odd.
e.g.
101100 // even
& 000001
= 000000 // false
101101 // odd
& 000001
= 000001 // true
i & 1
is the same as i % 2 != 0
, so it's checking if the number is odd, but in an obfuscated way. Don't do that. Your compiler knows how to optimize such things already.
That is not a language nor container specific operation.
That is a typical operation to know if an integer is ODD or EVEN,
performing the bitwise and operation for the bit 1 will only be true in ODD integers.
int odd=3; //binary 011
int even=4; //binary 100
if (odd&1)
printf ("odd number");
if (!(even&1))
printf ("even number");