LoginSignup
0
0

More than 1 year has passed since last update.

XORで大文字小文字を入れ替える

Posted at

既出な気もしますが、AND演算とOR演算を使ったASCIIコードの大文字小文字変換の勉強中にXORで大文字と小文字を行ったり来たりできることを見つけたのでそのメモです。

0b0010_0000とXORすることで大文字なら小文字に、小文字なら大文字に変換できます。

サンプルコード.py
// 2進数表記
print("0b" + format(0b0100_0001 ^ 0b0010_0000, "08b"))
// -> 0b01100001

print("0b" + format(0b0110_0001 ^ 0b0010_0000, "08b"))
// -> 0b01000001


// 16進数表記
print(hex(0x41 ^ 0x20))
// -> 0x61

print(hex(0x61 ^ 0x20))
// -> 0x41
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0