Bit Manipulation Application Small to Capital Letter

suggest change

One of several applications of bit manipulation is converting a letter from small to capital or vice versa by choosing a mask and a proper bit operation. For example, in ASCII the letter a has the representation 01(1)00001 while A has 01(0)00001. They differ only in the bit in parentheses. Converting a to A clears that bit. To do so, AND with a mask that has 0 only in that position:

/****************************************
convert small letter to capital letter.
========================================
     a: 01100001
  mask: 11011111  <-- (0xDF)  11(0)11111
      :---------
a&mask: 01000001  <-- A letter
*****************************************/

The code for converting a letter to A letter is:

// :glot
#include <cstdio>

int main()
{
    char op1 = 'a';  // "a" letter (i.e. small case)
    int mask = 0xDF; // choosing a proper mask

    printf("a (AND) mask = A\n");
    printf("%c   &   0xDF = %c\n", op1, op1 & mask);
    
    return 0;
}
a (AND) mask = A
a   &   0xDF = A

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents