C Codes

Case Conversion using Bitwise

Upper Case to Lower Case:


#include
#include 

/*
Letter    ASCII     Binary
---------------------------
A          65       1000001
a          97       1100001
There is a difference of 32. Hence we OR it with 32 to convert the case
*/
int main()
{
    char c = 'A';
    int x = 32;
    c = c | 32;
    printf("%c\n", c);
}

Lower Case to Upper Case:


#include
#include 

/*
Letter    ASCII     Binary
---------------------------
a          97       1100001
A          65       1000001
There is a difference of 32. We AND it with ~32 to convert the case
*/

int main()
{
    char c = 'a';
    int x = 32;
    c = c & ~32;
    printf("%c\n", c);
}

Let me Know What you Think!