ASCIIでは
- A,C,G,Tの値は0x61,0x63,0x67,0x74なので,下位4bitを取り出すと1,3,7,4になり,すべて異なる値となる
- 大文字と小文字の値は0x20だけ違うため,下位4bitを取り出すと大文字と小文字は同じ値となる
→ 下位4bitだけ読むことでA,C,G,Tをcase-insensitiveに読める
int read_base(char c) {
switch (c & 0xf) {
case 'A' & 0xf: // 1
return 0;
case 'C' & 0xf: // 3
return 1;
case 'G' & 0xf: // 7
return 2;
case 'T' & 0xf: // 4
return 3;
}
return 0;
}
or
static const int conv[16] = {
['A' & 0xf] = 0, ['C' & 0xf] = 1, ['G' & 0xf] = 2, ['T' & 0xf] = 3
};
int read_base(char c) {
return conv[c & 0xf];
}