2

I came across this in the http_parser library of node.js implementation. When I bitwise-OR an ASCII capitalized character code, it converts into its lowercase version. The lowercase version remains as it is.

Why is a bitwise OR causing the ASCII code to change to the lowercase letter?

  • 3
    look at how the letters are layed out in the ascii table A is 0b100_0001 and a is 0b110_0001 – ratchet freak Jan 5 '15 at 15:49
  • 13
    Because the ASCII encoding was cleverly arranged in the 1960s so that precisely this operation would be simple. (Although I dare say they didn't imagine server-side JavaScript libraries back then.) – Kilian Foth Jan 5 '15 at 15:51
  • 2
    @KilianFoth and it's that clever arrangement that made it more attractive than EBCDIC (granted a similar bit trick is possible in EBCDIC but not much else and that made ASCII superior) – ratchet freak Jan 5 '15 at 16:48
  • 1
    @KilianFoth I suppose the idea of having client-side JavaScript was about as absurd as having server-side JavaScript in the 1960s. This reminds me of how certain languages used to be criticized for not being C-like despite the fact that C had not been invented. – toniedzwiedz Jan 6 '15 at 0:35
  • 1
    One of the votes to close is because "primarily opinion based". I'm unclear which part is opinion based, the binary ASCII codes, or the bitwise OR operation? – Rob Jan 6 '15 at 1:28
9

ASCII has several interesting properties in addition to the one you pointed out. It is relatively simply to ascertain properties about a character. For example, all printable characters are greater than or equal to 32 (010 0000), so if you AND a character with 96 (110 0000) and get zero, it must be nonprintable. Uppercase and lowercase letters differ by a single bit as you noticed:

A = 100 0001, OR with 010 0000 gives a = 110 0001. Same is true for all other letters.

a = 110 0001, AND with 101 1111 gives A = 100 0001. Same is true for all other letters.

This makes it trivial to perform case-insensitive searches via simple bit mask logic. Certainly it is much easier than with some Unicode-encoded characters that need to be decoded into their code points first (7-bit ASCII is also valid UTF-8, good luck other encodings).

Another cool property is the numbers all start with the high-order bits 011. If you see that, and the next bits are 0-9 (0000 - 1001) you can grab those bits and interpret them directly as an integer.

I cannot provide any specific quotes from decades ago back when EBCDIC and ASCII were competing standards, but it is common knowledge in the CS community that ASCII was designed with those specific properties in mind. The idea was to be able to use bit masking and arithmetic an easy way to detect and manipulate human-readable attributes of ASCII characters.

Many devices from ages ago were character-based, such as printers and terminals. Having an easy way for the low-tech devices of the day to be able to decode and use a character without a CPU was a bit deal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.