The difference between ASCII value of ‘A’(65) and ‘a’(97) is 32 i.e . This was intentionally designed in ASCII. Due to this following tricks can be used when dealing with alphabets(especially in C to write faster logic).

  • Add 32 to uppercase ASCII value of alphabet to get its corresponding lowercase value and subtract 32 from uppercase ASCII value of alphabet to gets its lowercase ASCII value. Talking in terms of bits, this is simplified by flipping 5th bit to get its corresponding lowercase/uppercase ASCII value.
  • Bit mask Alphabet ASCII value with 0x1f(32 in decimal and 0b11111 in binary) to get its corresponding alphabetical position. For example, 97 & 0x1f will be 1 and so will be 65 & 0x1f. Since the difference in the casing value’s of alphabets is 32, the rightmost 5 bits are same for same alphabet and hence masking it with 0x1f will give same value. Neat trick to check if value is alphabet without handling separately for both upper and lowercase conditions.