Bit Hacks – Part 4 (Playing with letters of English alphabet)

In this post we will discuss some of the bit hacks/tricks on letters of English alphabet.


Below tricks are covered in this post –



Trick 1. Convert uppercase character to lowercase

We can easily convert an uppercase character to corresponding lowercase character by taking its OR with a space.



Trick 2. Convert lowercase character to uppercase

Similarly, we can easily convert an lowercase character to corresponding uppercase character by taking its AND with an underscore character.



Trick 3. Invert alphabet’s case

Above solutions will not work the other way. i.e (ch | ‘ ‘) cannot convert lowercase alphabet to uppercase. The result will always lowercase even if letter is already lowercase, Similarly, (ch & ‘_’) cannot convert uppercase alphabet to lowercase. The result will always be uppercase even if letter is already uppercase.

How can we invert the alphabet’s case?

We can easily convert an alphabet’s case by taking its XOR with a space.



How does above solutions work?

The trick lies in ASCII codes of ‘A’-‘Z’ and ‘a’-‘z’ –

‘A’ – 01000001 ‘a’ – 01100001
‘B’ – 01000010 ‘b’ – 01100010
‘C’ – 01000011 ‘c’ – 01100011
‘D’ – 01000100 ‘d’ – 01100100
‘E’ – 01000101 ‘e’ – 01100101
and so on…

If we carefully analyze, we will notice that ASCII codes of lowercase and uppercase characters differ only in their third significant bit. For uppercase characters, the bit is 0 and for lowercase characters the bit is 1. If we could find a way to set/unset that particular bit, we can easily invert case of any character. Now space ‘ ‘ has ASCII code of 00100000 and ‘_’ has ASCII code of 01011111.

  • If we take OR of an uppercase characters with ‘ ‘, the third significant bit will be set and we will get its lowercase equivalent.
  • If we take AND of a lowercase character with ‘_’, the third significant bit will be unset and we will get its uppercase equivalent.
  • If we take XOR of an uppercase or lowercase characters with ‘ ‘ (ASCII 00100000), only its third significant bit will be toggled. i.e. lowercase becomes uppercase and vice versa.


Trick 4. Find letter’s position in alphabet

We can easily find a letter’s position [1-26] in the alphabet by taking its AND with ASCII 31 (00011111 in binary).

For example,

(‘A’ & 31) returns position 1
(‘c’ & 31) returns position 3

Note that case of the letter is irrelevant here. The explanation is left for the users as an exercise.


Read More –

Bit Hacks – Part 1 (Basic)
Bit Hacks – Part 2 (Playing with k’th bit)
Bit Hacks – Part 3 (Playing with rightmost set bit of a number)
Bit Hacks – Part 5 (Find absolute value of an integer without branching)
Bit Hacks – Part 6 (Random Problems)

Useful links –
ASCII Text to Binary Converter
Binary to ASCII Text Converter

Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂