Skip to content

Instantly share code, notes, and snippets.

@AkiyukiOkayasu
Last active November 24, 2022 13:39
Show Gist options
  • Save AkiyukiOkayasu/85b8cfc47823f2ddf9112e39cfd122c0 to your computer and use it in GitHub Desktop.
Save AkiyukiOkayasu/85b8cfc47823f2ddf9112e39cfd122c0 to your computer and use it in GitHub Desktop.
ビット演算の基礎

ビット演算の基礎

  • 8bitのデータを上位4bitと下位4bitに分割する

元となる8bitのデータをxとし、上位4bitをa、下位4bitをbとします

//擬似コード
a = (x >> 4) & 0b1111;
b = x & 0b1111;
  • 分割
    input: x (16bit)
    output: a (上位8bit)、 b (下位8bit)
//擬似コード
a = (x >> 8) & 0xFF;
b = x & 0xFF;

  • 上位4bitと下位4bitから8bitのデータを作る

反対に上位4bitと下位4bitから8bitのデータを作る場合

//擬似コード
x = (a << 4) | (b & 0b1111)
  • 結合
    input: a (上位8bit)、 b (下位8bit)
    output: x (16bit)
//擬似コード
x = (a << 8) | (b & 0xFF)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment