set
a hash set in nim?int getCount(const std::string& inputStr){
int num_vowels = 0;
for (auto i : inputStr){
num_vowels += (0x208222 >> (i & 0x9F)) & 0x1;
}
return num_vowels;
}
(0x208222 >> (i & 0x1f)) & 0x1
?>>> "".join([chr(i) for i in range(128) if (0x208222 >> (i & 0x1F)) & 0x1])
'\x01\x05\t\x0f\x15!%)/5AEIOUaeiou'
(edited)i
&ing 0x9F is just subtracting 96
because 0x9f = 1001'1111, and &ing this removes the 64 and 32 bit (which adds to 96)
it's useful because 'a' == 97, so it's just one off
0x208222 is 1000001000001000100010 in binary
which has 5 binary digits at positions : 1
, 5
, 9
, 14
and 21
this corresponds to the position of vowels after 96 :
'a' = 97 + 0 == 96 + 1
'e' = 97 + 4 == 96 + 5
'i' = 97 + 8 == 96 + 9
'o' = 97 + 14 == 96 + 14
'u' = 97 + 20 == 96 + 21
so if after rightshifting 0x208222 by (i
- 96) the last bit is 1
,it must be a vowel (edited)1
at the last bit . it doesn't matter for this string range, but this could lead to undefined behavior (ISO 9899:2011 6.5.7 Bitwise shift operators) which is avoidable, so you are correct that I should use 0x1F instead of 0x9F (edited)