algorithms

Case Conversion using Bitwise – C Programs

Refer to theory HERE.

Program to Convert Upper Case to Lower Case:

#include <stdio.h>
#include <stdlib.h>

/*
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);
}

Program to Convert Lower Case to Upper Case:

#include <stdio.h>
#include <stdlib.h>

/*
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);
}
Advertisements

Let me Know What you Think!

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s