Task: Find the set E containing all lowercase Russian letters from A and B, but not common to the sets C and D. you need to do this all through an array of bits. So I understand that I will need to translate char into binary code. There is only one idea: to make an array of char X [] and fill it with lowercase Russian letters, then enter each set of A, B, C, D from the keyboard, and check each element of a separate set with X and if the same element of the set matches in "true", this, of course, is not entirely rational, but I study programming recently, therefore, unfortunately, there are no other ideas. The question is different, how can I get a lot of E? Are there any relevant operators? programming level, I repeat, the initial one, so do not offer, please, too "abstruse" options).
1 answer
@skies , what Russian encoding (single-byte, utf-8 or UCS) do you work with?
In any case, Russian lowercase letters can be numbered 0..32 (for example, а
- 0, б
- 1, ... я
- 31, ё
- 32) and use a 64-bit integer (unsigned long long or uint64_t) to represent this sets.
Then to add a letter (its number n) to the set A, write:
A |= (1<<n);
to exclude a letter from the set
A &= ~(1<<n);
to check if there is a letter in the set (whether the bit is set) use the expression
(A & (1<<n))
Union of 2 sets (A and B)
AB = A | B; CD = C | D;
Then the letters that are in AB and that are missing in the CD are
E = AB & ~ CD;
I will explain. In CD, bits are set for each of the letters that are in C or D. The operation ~ inverts all bits. Operation & will reset all bits in AB, for which bits are reset in CD, and the rest will be left unchanged .
IMHO everything.