What is the idea behind ^= 32, that converts lowercase letters to upper and vice versa?

前端 未结 10 1812
孤城傲影
孤城傲影 2020-11-28 03:02

I was solving some problem on codeforces. Normally I first check if the character is upper or lower English letter then subtract or add 32 to convert it to the

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 03:34

    Let's take a look at ASCII code table in binary.

    A 1000001    a 1100001
    B 1000010    b 1100010
    C 1000011    c 1100011
    ...
    Z 1011010    z 1111010
    

    And 32 is 0100000 which is the only difference between lowercase and uppercase letters. So toggling that bit toggles the case of a letter.

提交回复
热议问题