Cool way to to convert Lowercase Letters to Uppercase and vice versa using Bit masking

Revision en1, by theking7, 2022-01-15 12:19:17

code :

// Converting Upper to lower and Vice Versa 1) If we do 1 left shift 5 and if we toggle the bits and take a '&' of it, we can turn the lower case to Upper Case.

cout << char('c' & ~( 1 << 5)) << endl;

2) If we do 1 left shift 5 and if we take a '|' of it, we can turn the Upper case to Lower Case.

cout << char('C' | 1 << 5) << endl;

3) More Cooler Trick — i) 1 left shift 5 can be switched with the ASCII value of space i.e. ' '.

cout << char('c' & ~(' ')) << endl;
cout << char('C' | ' ') << endl;
Tags bitmask, tricks, lower to upper, upper to lower

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English theking7 2022-01-15 12:19:17 652 Initial revision (published)