Change case of a character using bit manipulation in C++

Here, we will learn how to change case of a character using bit manipulation in C++? Program will demonstrate lowercase to uppercase and uppercase to lowercase conversion.
Submitted by Hritik Raj, on June 24, 2018 [Last updated : February 28, 2023]

Changing the case of a character using bit manipulation

Problem statement:

Change case of a character (Using Bit Manipulation)

Lowercase to uppercase

Example:

    input:  a
    output: A

Uppercase to lowercase

Example:

    input:  A
    output: a

Using Bit manipulation we can change case of a character very easily and efficiently, as we know every character is stored in memory as their ASCII code which is an Integer value

Here is Binary representation of Some ALPHABETs in uppercase as well as in their lowercase.

Alphabet   ASCII code   Binary form
  A           65           1000001
  a           97           1100001

  B           66           1000010
  b           98           1100010

  Z            90          1011010
  z           122          1111010

If we look carefully we will find that only 5th bit (considering 0 based indexing) is different in the binary representation of Uppercase and lowercase of a particular character (Alphabet).

Converting lowercase to uppercase

We need to clear the 5th bit of the given character to make it in uppercase.

To clear 5th bit in the given character, we can use BITWISE AND ( & ) operator as follow

    char = char & (~(1 << 5));
    it is equivalent to...
    char = char & 95
    95 is ASCII value of '_' ( Underscore)
    so we can write it as
    char = char & '_';

Example:

    Let's take char
    'a' =  97 ( 1100001 in binary )
    '_' =  95 ( 1011111 in binary )
    a & '_'  =  65 ( 1000001  in binary )

    And 65 is ASCII code for 'A' which is uppercase version of 'a'

Converting uppercase to lowercase

We need to set the 5th bit of the given character to make it in lowercase.

To set 5th bit in the given character we can use BITWISE OR (|) operation as follow: char = char | ( 1 << 5 );

Example:

    Let's take  char
    A  = 65 ( 1000001 in binary )
    (1 << 5 )  = 32 ( 0100000 in binary )

    A | 32  = 97 ( 1100001  in binary )

    And 97 is ASCII code for 'a' which is lower case version of A

C++ code to change the case of a character using bit manipulation

#include <iostream>
using namespace std;


char lowerToUpper(char c)
{
	return (c & '_');
}

char upperToLower(char c)
{
	// ASCII value of space (' ') is 32 
	// so it is equivalent to (1 << 5 )
	return (c | ' ');
}

int main()
{
	char c;
	cout << "Enter Character ( in lower case ) : \n";
	cin >> c;
	cout << "Output :  "  << lowerToUpper(c);
	cout << "\n\nEnter Character ( in Upper case ) : \n";
	cin >> c;
	cout << "Output :  "  << upperToLower(c);

	return 0;
}

Output

    Enter Character ( in lower case ) :
    a
    Output :  A

    Enter Character ( in Upper case ) :
    B
    Output :  b
    Process returned 0 (0x0)   execution time : 5.769 s
    Press any key to continue.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.