CodeISM Class 12 (Bit Manipulation)
CodeISM Class 12 (Bit Manipulation)
Note that & (bitwise AND) is different from && (logical AND) , | (bitwise OR)
is different from || (logical OR) and ~ (bitwise NOT) is different from !
(logical NOT)
Rules->
AND
0&0=0
0&1=0
1&0=0
1&1=1
OR
0&0=0
0&1=1
1&0=1
1&1=1
XOR
0&0=0
0&1=1
1&0=1
1&1=0
Properties of XOR:
a^a=0
0^a=a
NOT
~0=1
~1=0
Ans: a*(2b)
Q.) You are given a number and find the value of (a>>b)?
Ans: a/(2b)
(2^3+2^1)=2(2^2+2^0)=2*a;
Q.) You are given an array of N numbers in which all the numbers are
repeated twice except one number which is present exactly once then find
out that number?
Link: https://www.hackerrank.com/challenges/lonely-integer/problem
int main()
{
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++) cin>>arr[i];
int ans = 0;
for(int i=0;i<n;i++){
ans=ans^arr[i];
}
cout<<ans;
}
Q.) You have to check whether the given number is odd or even but you are
not allowed to use % operator then how do you do that?
a=5-> (0101)
b=10-> (1010)
a&1=>(0101)&(0001)=(0001)=1
b&1=>(1010)&(0001)=(0000)=0
if((a&1)==1){
cout<<"ODD"<<endl;
}
else {
cout<<"EVEN"<<endl;
}
Q.) How to check whether ith bit (from right) is 1 or 0 for the given input
number n.
001010101010
….43210 (index)
0->0
1->1
(1<<i) -> 000000010000000
i
Answer:
if(n&(1<<i))
{
cout<<"i-th bit is set"<<endl;
}
else
{
cout<<"i-th bit is not set"<<endl;
}
Q.) How to set the ith bit to 1 for the given input number n;
Ans:
n = n|(1<<i);
Q.) How to set the ith bit to 0 for the given input number n;
Ans:
n=n&(~(1<<i))
a-> 0000010101010100000
i
(1<<i) 0000000010000000000
~(1<<i) 111111111011111111111
Q.) How to generate all possible non empty subsequences of the given
string.
e.g-> (abc) has following subsequences:
a,b,c,ab,bc,ac,abc
Subsequence -> delete some elements from anywhere in the string and
concatenate the remaining.
Eg abcdefgh -> del c,f,h -> abdeg
Substring -> delete some elements from the beginning and some from the end.
Eg abcdefgh -> del a,b from begin and g,h from end
-> cdef
Sol :-
Assume that length of string is n.
Represent any subsequence of this string as a binary number of length n.
Eg abc -> binary number of length 3
If binary digit is 1 -> then that char is present in this sequence.
Else it is deleted.
abc
110 -> ab 1 to 7 -> 001,010,011,100,101,110,111
-> c, b , bc, a ,ac ,ab , abc
101-> ac
N -> 1 to 2^n-1
vector<string> seq;
// stores all non empty subsequences
string s;
cin>>s;
int n = s.length();
//n<=15
for(int i=1;i<(1<<n);i++){ // 1 to 2^n-1
string temp="";
for(int j=0;j<n;j++){
if(i&(1<<j)){ // if jth bit in i is set
temp+=s[j];
}
}
seq.push_back(temp);
}
for(int i=0;i<seq.size();i++){
cout<<seq[i]<<" ";
}
Q. https://www.hackerrank.com/challenges/and-product/problem
while(a){
int x = a%2;
x+=('0'+x);
x/=2;
}
reverse(x.begin(),x.end());
Q. https://www.hackerrank.com/challenges/sansa-and-xor/problem
345
3
4
5
3,4
4,5
3,4,5
Answer=> (3)^(4)^(5)^(3^4)^(4^5)^(3^4^5)