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)
Similarly,
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”