c/c++ 大小写转换(位运算)

#include <iostream>

// 大写转小写  小写转大写
static char UpperOrLower(char ch) {
    return (ch ^ 0x20);
}

// 转大写
static char ToUpper(char ch) {
    // 0xDF => 11011111
    return (ch & 0xDF);
}

// 转小写
static char ToLower(char ch) {
    // 0x20 => 00100000
    return (ch | 0x20);
}


int main()
{
    char chUpper = 'P';

    char chLower = ToLower(chUpper);
    std::cout << chLower << std::endl;

    chLower = 'm';
    chUpper = ToUpper(chLower);
    std::cout << chUpper << std::endl;
    return 0;
}

 

posted @ 2022-12-27 20:12  炎叶  阅读(128)  评论(0)    收藏  举报