Skip to content

Instantly share code, notes, and snippets.

@0xbadfca11
Created July 7, 2018 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xbadfca11/6708718f45496dc22095339ffff8a47a to your computer and use it in GitHub Desktop.
Save 0xbadfca11/6708718f45496dc22095339ffff8a47a to your computer and use it in GitHub Desktop.
母音の判定

char a = <...>;
2130466 >> a & 1;
とは

ネタ元

https://twitter.com/kumagi/status/1014953960237436928

必要前提知識

x86のシフト命令は下位5ビットしか見ない

The count operand can be an immediate value or the CL register. The count is masked to 5 bits (or 6 bits if in 64-bit mode and REX.W is used). The count range is limited to 0 to 31 (or 63 if 64-bit mode and REX.W is used).
Intel® 64 and IA-32 architectures software developer's manual volume 2B

x86アーキテクチャ以外で試す場合や未定義動作を避ける場合はビットマスクなり剰余なりが必要。

ASCIIコード

a-zA-Zを下位5ビットのみで表すとこうなる

ascii decimal mod 32
A 65 1
B 66 2
C 67 3
E 69 5
I 73 9
O 79 15
U 85 21
X 88 24
Y 89 25
Z 90 26
a 97 1
b 98 2
c 99 3
e 101 5
i 105 9
o 111 15
u 117 21
x 120 24
y 121 25
z 122 26

小文字がちょうど大文字から+32の位置にあるのが美しいね

2130466

2130466 == 1 << 'A' % 32 | 1 << 'I' % 32 | 1 << 'U' % 32 | 1 << 'E' % 32 | 1 << 'O' % 32

感想

理解はできても思いつかない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment