java - How do I get the numerical value/position of a character in the alphabet (1-26) in constant time (O(1)) without using any built in method or function? -
how numerical value/position of character in alphabet (1-26) in constant time (o(1)) without using built in method or function , without caring case of character?
if compiler supports binary literals can use
int value = 0b00011111 & character;
if not, can use 31 instead of 0b00011111 since equivalent.
int value = 31 & character;
or if want use hex
int value = 0x1f & character;
or in octal
int value = 037 & character;
you can use way represent value 31.
this works because in ascii, undercase values prefixed 011, , uppercase 010 , binary equivalent of 1-26. using bitmask of 00011111 , and operand, covert 3 significant bits zeros. leaves 00001 11010, 1 26.
Comments
Post a Comment