Created
September 8, 2016 16:13
-
-
Save dasjestyr/e2a3cbb8f07f49686ae29f175d607b51 to your computer and use it in GitHub Desktop.
Bit Masking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| /* use XOR to toggle bits */ | |
| var value = 0x11; // 00010001 | |
| var mask = 0xFF; // 11111111 (flip everything; make a mirror) | |
| var result = value ^ mask; // 11101110 | |
| OutputAsBinary(result); | |
| /* use OR to set bits (to true) */ | |
| value = 0x11; // 00010001 | |
| mask = 0x2; // 00000010 | |
| result = value | mask; // 00010011 | |
| OutputAsBinary(result); // 00010011 | |
| /* use AND with NOT to unset a bit (set to false) */ | |
| value = 0x11; // 00010001 | |
| mask = 0x1; // 00000001 (unset the first bit) | |
| result = value & ~mask; // 00010000 | |
| OutputAsBinary(result); | |
| /* use AND to check a bit */ | |
| value = 0x11; // 00010001 | |
| mask = 0x10; // 00010000 (requires fifth bit = true) | |
| var hasBits = (value & mask) == mask; // if the bit(s) are present, then the result will be equal to the mask | |
| Console.WriteLine(hasBits); | |
| // check if multiple bits are present (same thing) | |
| value = 0x39; // 00111001 | |
| mask = 0x18; // 00110000 (requires 4th and fifth bits = true) | |
| var hasFlags = (value & mask) == mask; | |
| Console.WriteLine(hasFlags); | |
| /* Extract pieces of a larger word */ | |
| value = 0x3F2C; // 0011 1111 0010 1100 | |
| mask = 0xFF; // 0000 0000 1111 1111 (one byte mask. Everything to the left is 'dont care') | |
| var firstByte = value >> 8; // 0000 0000 0011 1111 (move it into byte 1 position and mask out everything else) | |
| var secondByte = value & mask; // 0000 0000 0010 1100 (already in byte 1 position) | |
| Console.WriteLine("\r\nSmall word word extraction example: "); | |
| OutputAsBinary(value); | |
| OutputAsBinary(firstByte); | |
| OutputAsBinary(secondByte); | |
| /* larger extraction example */ | |
| Console.WriteLine("\r\nLarge word extraction example: "); | |
| value = 0x32E2CD1D; // 0011 0010 1110 0010 1100 1101 0001 1101 (32 bit integer) | |
| mask = 0xFF; // 0000 0000 1111 1111 | |
| var first = value >> 24; // 0011 0010 | |
| var second = (value >> 16) & mask; // 1110 0010 | |
| var third = (value >> 8) & mask; // 1100 1101 | |
| var fourth = value & mask; // 0001 1101 | |
| OutputAsBinary(value); | |
| OutputAsBinary(first); | |
| OutputAsBinary(second); | |
| OutputAsBinary(third); | |
| OutputAsBinary(fourth); | |
| } | |
| public static void OutputAsBinary(int input) | |
| { | |
| var asHex = input.ToString("X2"); | |
| var asBinary = Convert.ToString(input, 2).PadLeft(8, '0'); | |
| Console.WriteLine($"{asBinary} (0x{asHex})"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment