## Character case conversion using Bitwise operators

Posted by appusajeev on July 12, 2009

I found this somewhere and hope it might come handy.

ASCII(American Standard Code for Information Interchange) is a method commonly used to represent characters(including alpha numerals,punctuations,control characters) in memory.In this method,each character is uniquely defined by a 7-bit number(or 8 to include some extra characters). For eg. the ASCII code for ‘A’ is 65 and that of ‘a’ is 97 and that of ‘#’ is 35.

There exists a striking similarity between the ASCII values of upper case alphabets and lower case alphabets . The binary representation of an upper case alphabet and the lower case of the same are the same except for the fifth bit !

for eg . the ASCII code of ‘ B’ is 66 which is **1 ***0*** 0 0 0 0 1 ** in binary

and the ASCII code of ‘b’ is 98 which is **1 ***1* ** 0 0 0 0 1 **in binary .

Notice that there is a change only in the fifth bit and the rest are the same.

**The fifth bit is set for a lower case alphabet and it is cleared for an upper case alphabet**.

With that knowledge,bitwise operators can be used to detect if the character is in upper case of not.

The python statement to detect the case is (assuming *x* contains an alphabet)

**ord(x) & (1<<5)**

If the above statement is true,the character contained in *x* is a lower case character.* ord()* is a built in function which returns the ASCII code of a character.

and the corresponding C statement would be **: **

** x & (1<<5) **

The advantage of this method is that there is no need to the check for the range of ASCII values to determine if the character is upper case or not.

Now to character conversion. Suppose that *a* contains a lower case character. To convert it into upper case,just set the fifth bit to zero . The following python code illustrates the method

**a=chr(ord(a)&~(1<<5))**

and the corresponding C code would be

**a=a &~(1<<5))**

This method may sound lame when the easier method to add 32 to the ASCII code exists but it just demonstates the application of bitwise operators.

## neo2904 said

thats a cool method..

## Kain said

Don’t you mean the SIXTH bit?

And yeah you can do the same, easier by using a bitwise AND with the value of ‘_’ or 95 (hex 5f)

Thus y = x & 0x5f

where x == ‘a’

y == ‘A’

also a bitwise exclusive OR with the value of ‘ ‘ (32, or 0x20) will do the inverse function

Thus y = x ^ 0x20

where x == ‘A’

y == ‘a’

## Kain said

Actually you could just use the exclusive OR to toggle the bit either way.

## Kain said

and a normal bitwise OR to simply clear the bit

## appusajeev said

sixth bit if your counting starts from 1 and 5th bit if the count starts from 0 …

## Silas S. Brown said

Adding 25 to the ASCII code works only if you know the letter is upper case. Setting the bit works whatever the current case of the letter. So the bitwise technique saves a conditional. As long as you don’t mind converting @[\]^_ to `{|}~delete

## Silas S. Brown said

When you say add 25 to the ASCII code, surely you mean 32 (hex 20), not 25?

## appusajeev said

Ah yes, my bad. Will correct that. Thanks.