Bitwise case conversion

Ryan Graham
2 min readApr 30, 2020

I recently had to brush up on character encoding. I hate even uttering that phrase. Not because it implies I keep relearning the same things, pasting the results into Evernote, and then forgetting until it becomes relevant again five years later. No, I hate it because I wish the whole world used unicode and everything around it was abstracted away into neat little library functions.

I lied. I secretly love ASCII and every sneaky little trick that goes with it. Just like I love x86 assembly language, Tcl, and the low resolution snow flake screensaver from my old FreeBSD desktop. They all remind me of my childhood even though I rarely use them professionally.

This is one of those aforementioned tricks. You can use XOR on an 8bit ASCII code to toggle between upper and lower case. Should you? Probably not. But it can be useful to understand how it works.

I’m going to use iex in my shell to demonstrate. I picked iex simply because I’ve been using it for work lately and it feels familiar. You could just as easily use interactive ruby, python, etc. The concept is the same. In fact we will write the same thing in C++ shortly.

IEX

Do you see the pattern? The sixth bit from the right is always set for lower case and unset for upper case. Do a bitwise xor of 32 and you can toggle that bit on and off going back and forth between upper and lower case.

So lets try implementing that in C…

Now let’s run it.

Isn’t that neat? Now what should I make for second dinner? It’s almost midnight, but it’s also quarantine season. I’m feeling pancakes.

PS: don’t write code like this in your real projects.

--

--