Details
Basics
At the heart of bit manipulation are the bit-wise operators & (and), | (or), ~ (not) and ^ (exclusive-or, xor) and shift operators a << b and a >> b.
There is no boolean operator counterpart to bitwise exclusive-or, but there is a simple explanation. The exclusive-or operation takes two inputs and returns a 1 if either one or the other of the inputs is a 1, but not if both are. That is, if both inputs are 1 or both inputs are 0, it returns 0. Bitwise exclusive-or, with the operator of a caret, ^, performs the exclusive-or operation on each pair of bits. Exclusive-or is commonly abbreviated XOR.
- Set union A | B
- Set intersection A & B
- Set subtraction A & ~B
- Set negation ALL_BITS ^ A or ~A 【各位取反: ALL_BITS^A 或者 ~A】、【A的复数形式:~A+1】
- Set bit A |= 1 << bit
- Clear bit A &= ~(1 << bit)
- Test bit (A & 1 << bit) != 0
- Extract last bit A&-A or A&~(A-1) or x^(x&(x-1)) 【得到最后一位:A&-A、 A&~(A-1) 、 x^(x&(x-1))】
- Remove last bit A&(A-1) 【删除最后一个1:A&(A-1)】
- Get all 1-bits ~0
Examples
n的二进制表示中1的个数
1 int count_one(int n) { 2 while(n) { 3 n = n&(n-1); //删除最后一个1 4 count++; 5 } 6 return count; 7 }
n是不是4的幂 return Integer.toString(n,4).matches("^10*$");
1 bool isPowerOfFour(int n) { 2 return !(n&(n-1)) && (n&0x55555555); 3 //check the 1-bit location; 4 }
^
tricks
Use ^
to remove even exactly same numbers and save the odd, or save the distinct bits and remove the same.
Sum of Two Integers
对于二进制的加法运算,若不考虑进位,则1+1=0,1+0=1,0+1=1,0+0=0,通过对比异或,不难发现,此方法与异或运算类似。因而排出进位,加法可用异或来实现。然后考虑进位,0+0进位为0,1+0进位为0,0+1进位为0,1+1进位为1,该操作与位运算的&操作相似。
那么加法运算可以这样实现:
1)先不考虑进位,按位计算各位累加(用异或实现),得到值a;
2)然后再考虑进位,并将进位的值左移,得值b,若b为0,则a就是加法运算的结果,若b不为0,则a+b即得结果(递归调用该函数)。
1 int getSum(int a, int b) { 2 return b==0? a:getSum(a^b, (a&b)<<1); //be careful about the terminating condition; 3 }
1 public class Solution { 2 public int getSum(int a, int b) { 3 if(b == 0) { //不产生进位时,相加完毕 4 return a; 5 } 6 int c = a ^ b; //按位相加,不进位 7 int d = (a & b) << 1; //按位相加后产生的进位 8 return getSum(c, d); 9 } 10 }
另:
-
不用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C),求1+2+3+···+n的值。
-
不用加法就用上面的位运算。
1 public class Solution { 2 int sum = 0; 3 public int getSum(int n) { 4 sum += n; 5 boolean falg = (n >0) && (sum = getSum(--n) > 0 ); 6 return sum; 7 } 8 }
Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. (Of course, you can do this by math.)
1 int missingNumber(vector<int>& nums) { 2 int ret = 0; 3 for(int i = 0; i < nums.size(); ++i) { 4 ret ^= i; 5 ret ^= nums[i]; 6 } 7 return ret^=nums.size(); 8 }
|
tricks
Keep as many 1-bits as possible
Find the largest power of 2 (most significant bit in binary form), which is less than or equal to the given number N.
1 long largest_power(long N) { 2 //changing all right side bits to 1. 3 N = N | (N>>1); 4 N = N | (N>>2); 5 N = N | (N>>4); 6 N = N | (N>>8); 7 N = N | (N>>16); 8 return (N+1)>>1; 9 }
Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
Solution
1 uint32_t reverseBits(uint32_t n) { 2 unsigned int mask = 1<<31, res = 0; 3 for(int i = 0; i < 32; ++i) { 4 if(n & 1) res |= mask; 5 mask >>= 1; 6 n >>= 1; 7 } 8 return res; 9 } 10 uint32_t reverseBits(uint32_t n) { 11 uint32_t mask = 1, ret = 0; 12 for(int i = 0; i < 32; ++i){ 13 ret <<= 1; 14 if(mask & n) ret |= 1; 15 mask <<= 1; 16 } 17 return ret; 18 }
&
tricks
Just selecting certain bits
Reversing the bits in integer
x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1); x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2); x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4); x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8); x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);
Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4.
Solution
1 int rangeBitwiseAnd(int m, int n) { 2 int a = 0; 3 while(m != n) { 4 m >>= 1; 5 n >>= 1; 6 a++; 7 } 8 return m<<a; 9 }
Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
Solution
1 int hammingWeight(uint32_t n) { 2 int count = 0; 3 while(n) { 4 n = n&(n-1); 5 count++; 6 } 7 return count; 8 } 9 int hammingWeight(uint32_t n) { 10 ulong mask = 1; 11 int count = 0; 12 for(int i = 0; i < 32; ++i){ //31 will not do, delicate; 13 if(mask & n) count++; 14 mask <<= 1; 15 } 16 return count; 17 }
Application
Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",
Return: ["AAAAACCCCC", "CCCCCAAAAA"].
Solution
1 class Solution { 2 public: 3 vector<string> findRepeatedDnaSequences(string s) { 4 int sLen = s.length(); 5 vector<string> v; 6 if(sLen < 11) return v; 7 char keyMap[1<<21]{0}; 8 int hashKey = 0; 9 for(int i = 0; i < 9; ++i) hashKey = (hashKey<<2) | (s[i]-'A'+1)%5; 10 for(int i = 9; i < sLen; ++i) { 11 if(keyMap[hashKey = ((hashKey<<2)|(s[i]-'A'+1)%5)&0xfffff]++ == 1) 12 v.push_back(s.substr(i-9, 10)); 13 } 14 return v; 15 } 16 };
But the above solution can be invalid when repeated sequence appears too many times, in which case we should use unordered_map<int, int> keyMap
to replace char keyMap[1<<21]{0}
here.
Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. (bit-counting as a usual way, but here we actually also can adopt sorting and Moore Voting Algorithm)
Solution
1 int majorityElement(vector<int>& nums) { 2 int len = sizeof(int)*8, size = nums.size(); 3 int count = 0, mask = 1, ret = 0; 4 for(int i = 0; i < len; ++i) { 5 count = 0; 6 for(int j = 0; j < size; ++j) 7 if(mask & nums[j]) count++; 8 if(count > size/2) ret |= mask; 9 mask <<= 1; 10 } 11 return ret; 12 }
Single Number III
Given an array of integers, every element appears three times except for one. Find that single one. (Still this type can be solved by bit-counting easily.) But we are going to solve it by digital logic design
Solution
1 //inspired by logical circuit design and boolean algebra; 2 //counter - unit of 3; 3 //current incoming next 4 //a b c a b 5 //0 0 0 0 0 6 //0 1 0 0 1 7 //1 0 0 1 0 8 //0 0 1 0 1 9 //0 1 1 1 0 10 //1 0 1 0 0 11 //a = a&~b&~c + ~a&b&c; 12 //b = ~a&b&~c + ~a&~b&c; 13 //return a|b since the single number can appear once or twice; 14 int singleNumber(vector<int>& nums) { 15 int t = 0, a = 0, b = 0; 16 for(int i = 0; i < nums.size(); ++i) { 17 t = (a&~b&~nums[i]) | (~a&b&nums[i]); 18 b = (~a&b&~nums[i]) | (~a&~b&nums[i]); 19 a = t; 20 } 21 return a | b; 22 } 23 ;
Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Example 1:
Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Return 16
The two words can be "abcw", "xtfn".
Example 2:
Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Return 4
The two words can be "ab", "cd".
Example 3:
Given ["a", "aa", "aaa", "aaaa"]
Return 0
No such pair of words.
Solution
Since we are going to use the length of the word very frequently and we are to compare the letters of two words checking whether they have some letters in common:
- using an array of int to pre-store the length of each word reducing the frequently measuring process;
- since int has 4 bytes, a 32-bit type, and there are only 26 different letters, so we can just use one bit to indicate the existence of the letter in a word.
1 int maxProduct(vector<string>& words) { 2 vector<int> mask(words.size()); 3 vector<int> lens(words.size()); 4 for(int i = 0; i < words.size(); ++i) lens[i] = words[i].length(); 5 int result = 0; 6 for (int i=0; i<words.size(); ++i) { 7 for (char c : words[i]) 8 mask[i] |= 1 << (c - 'a'); 9 for (int j=0; j<i; ++j) 10 if (!(mask[i] & mask[j])) 11 result = max(result, lens[i]*lens[j]); 12 } 13 return result; 14 }
Attention
- result after shifting left(or right) too much is undefined
- right shifting operations on negative values are undefined
- right operand in shifting should be non-negative, otherwise the result is undefined
- The & and | operators have lower precedence than comparison operators
Sets
All the subsets
A big advantage of bit manipulation is that it is trivial to iterate over all the subsets of an N-element set: every N-bit value represents some subset. Even better, if A is a subset of B then the number representing A is less than that representing B
, which is convenient for some dynamic programming solutions.
It is also possible to iterate over all the subsets of a particular subset (represented by a bit pattern), provided that you don’t mind visiting them in reverse order (if this is problematic, put them in a list as they’re generated, then walk the list backwards). The trick is similar to that for finding the lowest bit in a number. If we subtract 1 from a subset, then the lowest set element is cleared, and every lower element is set. However, we only want to set those lower elements that are in the superset. So the iteration step is just i = (i - 1) & superset
.
1 vector<vector<int>> subsets(vector<int>& nums) { 2 vector<vector<int>> vv; 3 int size = nums.size(); 4 if(size == 0) return vv; 5 int num = 1 << size; 6 vv.resize(num); 7 for(int i = 0; i < num; ++i) { 8 for(int j = 0; j < size; ++j) 9 if((1<<j) & i) vv[i].push_back(nums[j]); 10 } 11 return vv; 12 }
Actually there are two more methods to handle this using recursion
and iteration
respectively.
Bitset
A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...).
The class emulates an array of bool elements, but optimized for space allocation: generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char).
1 // bitset::count 2 #include <iostream> // std::cout 3 #include <string> // std::string 4 #include <bitset> // std::bitset 5 6 int main () { 7 std::bitset<8> foo (std::string("10110011")); 8 std::cout << foo << " has "; 9 std::cout << foo.count() << " ones and "; 10 std::cout << (foo.size()-foo.count()) << " zeros. "; 11 return 0; 12 }
来源:https://leetcode.com/problems/sum-of-two-integers/#/solutions