# Bitwise operations ### Bitwise Operator quizzes * [C Bitwise Operators Quiz](http://doc.callmematthi.eu/C_Bitwise_Operators.html) * [線上測驗](https://pconrad.github.io/old_pconrad_cs16/topics/bitOps/) (附上解答) ### Set a bit :::info where n is the bit number, and 0 is the least significant bit ::: ```cpp unsigned char a |= (1 << n); ``` Example: ``` a 1 0 0 0 0 0 0 0 a |= (1 << 1) = 1 0 0 0 0 0 1 0 a |= (1 << 3) = 1 0 0 0 1 0 0 0 a |= (1 << 5) = 1 0 1 0 0 0 0 0 ``` ### Clear a bit: ```cpp unsigned char b &= ~(1 << n); ``` - [ ] Example 1: ``` b 1 1 1 1 1 1 1 1 b &= ~(1 << 1) = 1 1 1 1 1 1 0 1 b &= ~(1 << 3) = 1 1 1 1 0 1 1 1 b &= ~(1 << 5) = 1 1 0 1 1 1 1 1 ``` - [ ] Example 2: Write a macro with two arguments a and pos, where a is a byte and the pos specifies a bit position of the byte. The macro should clear the particular bit position in the given byte. ```cpp #include <stdio.h> /* bit clear: a: int, pos: bit position to clear */ #define CLEARBIT(a, pos) (a &= ~(1 << pos)) int main() { /* 'z': decimal value 122 (=01111010) */ char a = 'z'; /* clearing the 5th bit */ char aOut = CLEARBIT(a, 5); /* aOut = 'Z': decimal value 90 (=01011010) */ printf("aOut=%c\n", aOut); return 0; } ``` ### Toggle a bit: ```cpp unsigned char c ^= (1 << n); ``` Example: ``` c 1 0 0 1 1 0 1 1 c ^= (1 << 1) = 1 0 0 1 1 0 0 1 c ^= (1 << 3) = 1 0 0 1 0 0 1 1 c ^= (1 << 5) = 1 0 1 1 1 0 1 1 ``` ### Test a bit: ```cpp unsigned char e = d & (1 << n); //d has the byte value. ``` ### The right/left most byte assuming 16 bit, 2-byte short integer: ```cpp unsigned char right = val & 0xff; /* right most (least significant) byte */ unsigned char left = (val >> 8) & 0xff; /* left most (most significant) byte */ ``` ### sign bit assuming 16 bit, 2-byte short integer, two's complement: ```cpp bool sign = val & 0x8000; // sign bit ``` ### Setting and Clearing a Bit The code below shows how to set or clear a bit of an integer. ```cpp #include <stdio.h> #include <stdbool.h> void binary(unsigned int n) { for (int i = 256; i > 0; i /= 2) { if (n & i) printf(" 1"); else printf(" 0"); } printf("\n"); } bool getBit(int n, int index) { return ((n & (1 << index)) > 0); } int setBit(int n, int index, bool b) { if (b) return (n | (1 << index)); int mask = ~(1 << index); return n & mask; } int main() { int num = 16, index; printf("Input\n"); for (int i = 7; i >= 0; i--) printf("%d ", getBit(num,i)); printf("\n"); /* set bit */ index = 6; printf("# Setting %d-th bit\n", index); num = setBit(num, index, true); for (int i = 7; i >= 0; i--) printf("%d ", getBit(num,i)); printf("\n"); /* unset (clear) bit */ index = 4; printf("# Unsetting (Clearing) %d-th bit\n", index); num = setBit(num, index, false); for (int i = 7; i >= 0; i--) printf("%d ", getBit(num,i)); printf("\n"); return 0; } ``` Output is: ``` Input 0 0 0 1 0 0 0 0 # Setting 6-th bit 0 1 0 1 0 0 0 0 # Unsetting (Clearing) 4-th bit 0 1 0 0 0 0 0 0 ``` 延伸閱讀: [Bitwise Operators in C](http://www2.mta.ac.il/~hbinsky/c%20content/Bits.pdf) * Uses of Bitwise Operations or Why to Study Bits * Compression * Set operations * Encryption * The Magic of XOR > `x ^ y == (~x & y) | (x & ~y)` --- ## Applications * [2018Q1 考古題](https://hackmd.io/s/rJh9U4Guf) / [考古題參考解答](https://hackmd.io/s/S1f0tSyTG) * [2018Q3 考古題](https://hackmd.io/s/S1a9-YO_Q) / [考古題參考解答](https://hackmd.io/s/By5KCaZam) * [2019Q1 考古題](https://hackmd.io/@sysprog/SyrZMGYr4) / [考古題參考解答](https://hackmd.io/@chenishi/S1DHQ3KrE) / [延伸閱讀](https://medium.com/fcamels-notes/%E4%BD%BF%E7%94%A8%E4%BD%8D%E5%85%83%E8%A8%88%E7%AE%97%E5%8A%A0%E9%80%9F%E6%B1%82%E8%A7%A3-n-queen-d20442f34110)