Converting Between Uppercase and Lowercase Using Bitwise Operations
Bitwise operations are a widely used computation method in computer science. When used appropriately, they can greatly improve computational efficiency. Today, I’d like to introduce a clever application of bitwise operations: converting between uppercase and lowercase letters.
To tackle this problem, we can first examine the ASCII code characteristics of uppercase and lowercase letters.
As we can see, the ASCII code values of each pair of uppercase and lowercase letters differ by 32. In binary, this means only the sixth bit is flipped while all other bits remain the same.
Therefore, by XORing a letter’s value with 0010 0000 (32), we can obtain its corresponding uppercase or lowercase letter.
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Comments

