# Bit operation/mask ###### tags: `Algorithm` ## Motivation 位元運算有很多技巧,可以改善時間複雜度和空間複雜度 ## Matrials 1. https://stackoverflow.com/questions/31575691/what-is-a-bitmask-and-a-mask 2. [Overall introduction](https://web.ntnu.edu.tw/~algo/Bit.html) 2. https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit 3. http://sean1223.blogspot.com/2009/04/cc.html 4. [XOR pattern](https://blog.techbridge.cc/2020/06/07/leetcode-pattern-bitwise-xor/) 1) XOR of any number n with itself gives us 0, i.e., n ^ n = 0 2) XOR of any number n with 0 gives us n, i.e., n ^ 0 = n 3) XOR is cumulative and associative. https://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/ * Addtion https://leetcode.com/problems/add-binary/discuss/1679423/Well-Detailed-Explaination-Java-C%2B%2B-Python-oror-Easy-for-mind-to-Accept-it ## Examples 1. [算有幾個1](https://blog.csdn.net/colouroo/article/details/60980816) 2.