Skip to content

Instantly share code, notes, and snippets.

@deeTEEcee
Created June 5, 2021 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deeTEEcee/0b7b61b62364a1e10dab49d6d9a4b77e to your computer and use it in GitHub Desktop.
Save deeTEEcee/0b7b61b62364a1e10dab49d6d9a4b77e to your computer and use it in GitHub Desktop.
bit tools

Hex-style bit mapping

Because bytes tend to be longer, we'll want to use hex style mapping to represent bits. We know that hexadecimal is base 16 and bits are base 2 which translates to 4 bits = 1 hex.

Here's an example of why we want this case. If you have 16 bits, it looks like: 000011111111 but 0xFF is easier to read.

Here's a list of mapping for wanting only 1 bits: 0 -> 0x0 1 -> 0x1 11 -> 0x3 111 -> 0x7 1111 -> 0xF ... repeat the pattern for the next digit 1 1111 -> 0x1F 11 1111 -> 0x3F 111 1111 -> 0x7F 1111 1111 -> 0xFF

It'll be good to have this pattern in your head if you're staring at a lot of logic which involves value extraction from x bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment