How to convert char to uppercase without using toupper function

I recently saw a piece of code that converts a char to lowercase, and if it is already in lowercase, it remains the same.

char c = 'A';
c |= ' ';
//c -> 'a'

I am trying to write code that can convert char to uppercase without using the toupper function .

Currently, the easiest way I can think of is with the code below.

char c = 'a';
c = (c | ' ') - ' ';
//c -> 'A'

So, I am wondering if there is code that is simpler than this and can achieve the same results.

Any help is appreciated.

A brief description of the first code block

Char | ASCII Code
' '  | 13
'A'  | 65
'a'  | 97

and operator orfor manipulating bits

   01000001 (char 'A')
Or 00100000 (char ' ')
 = 01100001 (char 'a')
----------------------
   01100001 (char 'a')
Or 00100000 (char ' ')
 = 01100001 (char 'a')
+4
source share
2

OR AND .

char c = 'a';
c &= ~' ';

DEMO

:

    01100001 (char 'a')
AND 11011111 (~ char ' ')
  = 01000001 (char 'A')
+9

- :

Live on coliru

#include <iostream>

char uppercase(char bla)
{
    return bla -('a'-'A');
}

int main()
{
  std::cout << uppercase('a') << '\n';
}

, a-z, if s. std::toupper unicode.

+1

Source: https://habr.com/ru/post/1622214/


All Articles