Bit manipulation is important part of competitive programming and is needed to solve a variety of problems.
To understand bit manipulation you need to know what bitwise operators are and how they work.
There are 6 types of bitwise operators:
Complement is probably the most least used bitwise operator.
Important note - To just clear the confusion I m using end and start like this - forEx(0001) 1 is at the end and 0 is at the start.
In binary form of any number every
011 bit. For ex - 16 = 10000Divide by 2 operation is similar to right shift by 1.
14 / 2 = 7 is same as 14(1110) >> 1 = 0111. Here 0111 == 7)Power Operation
(1 << x)Xor Operation
n simply do (x |= (1 << n)).
n in number x do (1 & (x >> n)).
x = 9 and n = 3.1001.1001 3 times will result in 0001.1 binary (0001) will result in 1.9 is set(1).1 as in above example otherwise 0.2 is (x & (1 << n)). But it doesn't return 1 or 0, so it can only be used in conditionals. Be careful while using it for something else.unset (0) the last bit (first from right) of any number x do (x & (x - 1)).
x - 1 will make just last bit 0 and then multiply it with original number i.e. x in this case.x - (x & - x)x ^ (x & (~x + 1))n in number x using x ^ (1 << n).2's complement of any decimal number x just by implicitly casting it to unsigned int.
unsigned int twosComplement = x;2's complement of any number x can be found just by -x. Simple as that.
x & -x.c & (~32).c | (32).c ^ 32c is some character. If c is digit it will throw garbage values.x & -x.Let two number be a and b.
If you take XOR of a and b and then count number of 1's in it. You will get minimum number of conversions to convert a to b.
Reason being is to convert the number a to b you have to find the positons at which A and B differs and you can do that by taking XOR.
For example convert 10 to 7.
1010 and 7 is 01111101.1101 is 3. Hence answer is 3.This example is from question 2220. Minimum Bit Flips to Convert Number.
We have to find the sum of xor of all the subsets.
So instead of generating all subsets We can find the total number of subsets as 2 power size_of_array. In bit it can be done as (1 << size_of_array).
The way we are getting all subsets is -
Consider a array of 3 elements [5, 6, 1]
If you will do 2 power 3 (here 3 is size of array) you will get 8. There are 8 possible subsets.
000
001
010
011
100
101
110
111The way it will help to consider all subsets is whenever there is 1 we will consider the element at that position to exist, and whenever 0 we will not consider it.
011011 element at both index 0 and 1 is 1.XOR of element at index 0 and 1 from array. In array index 0 is 5 and index 1 is 6. We are taking XOR since that's what we were asked to do in the question.int subsetXORSum(vector<int> &v)
{
int ans = 0;
for(int i = 0; i < (1 << v.size()); i++)
{
int x = 0;
for(int j = 0; j < v.size(); ++j)
if(i & (1 << j))
x ^= v[j];
ans += x;
}
return ans;
}There's a way to do this in O(N) time complexity but the problem is that I do not understand how @vortubac reached to that conclusion. But the code is pretty simple. Check his post here.
I will keep updating this article as I will find something interesting. If you find something wrong feel free to mention it in comment section.