Case Conversion using Bitwise – C

We generally use the functions strlwr() and strupr() available under string.h to toggle between the cases. This can also be achieved using bitwise operations. Before that, lets understand the ASCII table.

ASCII of A is 65 (1000001)
ASCII of a is 97 (1100001)

ASCII of Z is 97   (1011010)
ASCII of z is 122 (1111010)

From upper to lower cases we have one flipped bit in each case. Each of the case is separated by the distance of 32 (97-65 = 32). We can use this and do the case conversions as explained below:

Converting to Lower Case:

A     1000001
32   0100000
——————–  OR operation
a    1100001

Converting to Upper Case:

a        1100001
~32    1011111
———————  AND operation
A       1000001

One thought on “Case Conversion using Bitwise – C

Let me Know What you Think!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s