COMPLETED GUIDE ON BITWISE OPERATORS:)
LET`S DISCUSS ABOUT BITWISE OPERATORS ,IF U READ THIS ARTICLE U WILL GET SOLID KNOWLEDGE IN BIT MANIPULATION.LET`S GET STARTED.
IT IS A LONG ARTICLE ,BUT IF YOU READ IT WITH PATIENCE IT WILL HELP A LOT.
What Is Binary Number System?
Ans)It Is a Numbering System Mainly Used In Computer Field,
Bcoz Computers Does Not Understand Human Language .
And It Only Understands Electrical Signals Through Transistors.Hence Our Computer Scientist`s Introduced Binary System.
In This Binary Number System 1 Means Transistor Is On and 0 Means Transistor Is Off [Does Not Glow].
Let`s Discuss Some Operators Known As Bitwise Operators Which Manipulate the Numbers.They are:
1. Bitwise AND(&)
2. Bitwise OR(|)
3. Bitwise XOR(^)
4. Left Shift(<<)
5. Right Shift(>>)
6. ~
Let`s Discuss Them One By One With Some Examples.
1.Bitwise AND,Keyword="&";

What & Operator Does?

It takes Two Bits At A Time .IF Both Bits Are Set Then It Returns 1 Else Returns 0.
Lets See Truth Table.
[ 1 ] [ 1 ]  = 1
[ 1 ] [ 0 ]  = 0
[ 0 ] [ 1 ]  = 0
[ 0 ] [ 0 ]  = 0

Let`s See Some Examples .

1)Check Whether A Number Is Odd Or Not Without Using % Operator?

Note.*For Odd Numbers They Have 1st Right Bit As Set In Binary Representation.
     * For Even Numbers They Have 1st Right Bit As Set In Binary Representation.
When we Perform & With One[1], If We Get Result As 1 Then It Is Odd Number Else It Is Even.
 For Simplicity We Take 4 Bits To Represent.
			   0011
			 & 0001
			   ---
			   01 The Result Is 1 Hence It Is Odd.
			   
			   0010
			 & 0001
			 ------
			 00 The Result Is 0 Hence It Is Even.
			   
Let`s See Program

int main()
{
int a;
cin>>n;
if(c&1) cout<<"It Is Odd Number";

else cout<<"It Is Even Number".
}
2.Bitwise OR[ | ]

It Checks Two Bits At A Time .If Any Of The Bits Is Set It Results One ,If Both Are Zero It Results Zero. Let`s See Truth Table.

[ 1 ] [ 1 ]=[ 1 ]
[ 1 ] [ 0 ]=[ 1 ]
[ 0 ] [ 1 ]=[ 1 ]
[ 0 ] [ 0 ]=[ 0 ]
3.Bitwise XOR[ ^ ]

It is Important Operator In Bit Manipulation And Most Of The Interview Questions On Bit Manipulation Depends On XOR Operator.
It Takes Two Bits At a Time ,If Both Bits Are Different It Results One Else Zero.
Let`s See Truth Table

[ 1 ] [ 1 ]=[ 0 ]
[ 1 ] [ 0 ]=[ 1 ]
[ 0 ] [ 1 ]=[ 1 ]
[ 0 ] [ 0 ]=[ 0 ]

Let`s See Some Most Most Important Properties Of XOR.

1.XOR WITH ZERO RESULTS SAME NUMBER Ex:A^ZERO=A;

2.XOR WITH SAME NUMBER RESULTS ZERO Ex:A^A=0;

3.A^B==B^A;

4.A^(B^C)=B^(A^C)
 
 Let`s See Some Questions Related To XOR.
 
1)SINGLE NUMBER[136 IN LEETCODE]

DESCRIPTION :Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

LETS USE THE SECOND PROPERTY [XOR WITH SAME NUMBER IS ZERO].

 int single(vector<int>&nums)
 {
 int res=nums[0];
 for(int i=1;i<nums.size();i++)
 {
 res^=nums[i];
 }
 return res;
 }
 
 THE INTUITION IS THE NUMBER WHICH APPEARED TWICE WILL CANCEL EACH OTHER AND THE NUMBER WHICH HAS OCCURED ONCE WILL BE IN RES VARIABLE .
 
2)MINIMUM NUMBER OF FLIPS TO CONVERT A TO B.
FLIP means Changing 0 to 1 Viceversa.

We know How XOR works,Let Take XOR Of Two Numbers And Count Set Bits In The Result,
What The Intutin Behind Counting Set bits After Taking XOR ,If We Want To Convert A TO B we Want To Make ALL BIT Positions Of A equal To B,When We Take XOR If Two Bits Are Different Then Result Is One ,which Says That We Want To flip one Bit.
Let`s See The Program

int main()
{
int a,b,flips=0;
cin>>a>>b;
int res=a^b;
//counting set bits using predefined function;

flips=_builtin_pop_count(res)//A PreDefined Function In GCC Compiler To Count Number Of Set Bits In A Number,It returns Integer.

//counting set bits without predefined function.
while(res)
{
if(res&1==1)flips++;
res=res>>1;//RIGHT SHIFT OPERATOR SEE BELOW WE DISCUSSED CLEARLY.
}
cout<<flips;
}

3)Check Number Is Odd Or Even Using XOR Operator.

Let`s See The Program

int main()
{
int a;
cin>>n;
int res=a^1;
if(res==a-1)cout<<"GIVEN NUMBER IS ODD"

else cout<<"GIVEN NUMBER IS EVEN";
}
WHAT THE INTUTION 🤔🤔🤔I WILL LEAVE FOR U THINK WELL.
4.LEFT SHIFT OPERATOR[<<].

It Is Also An Important Operator.

THE MOTTO OF LEFT SHIFT OP IS IT SHIFTS THE BITS BY I POSITONS LEFT.Lets see How it Works.
Let`s Take A 8 BIT Number For Simplicity And that Number Is 3.
[0]0000011
now 3<<1 Means The Last BIT goes OFF AND New ZERO WIll BE added At right.
0000011[0]
        ☝  NEW BIT IS ADDED AT RIGHT.
Important Properties Of LEFT SHIFT Op are.

1)Left Shifting A Number i Times Then The Number Becomes N=N*pow(2,i);//predefined Pow Function.

2)If We Left Shift A Number By One It Is Same as Multiplying A Number By  2,that`s Why We Use It In Competitive Programming Bcoz Bitwise Op`s Are Faster Than Arthimetic Op`s.

LET`S See Some Questions ON Left Shift.

1)Multiply A Number With 2 Without Using "*" Operator.
Program
int main()
{
int a;
cin>>a;
int res=a<<1;
cout<<res;
}

2)Multiply A Given Number With 3?

It`s A tricky Question But It`s An Easy One Let`s Derive An Formula.
Let Consider A Number a=3,Now when Multiplied By 3 Then It Becomes 9.

Let`s Bring Some Math Here

 We Want  3*a 
 3a Can Be Written As 4a-a .
 
 Let`s Think How we Can Get 4a By USING LEFT SHIFT OP,[by left shifting 2 times am i correct]
 
 Let`s Write A Program.
 
 int main()
 {
 int a;
 cin>>a;
 int k=3;
 int temp=a;
 a=a<<2;//NOW A=12;
 cout<<a-temp;//CONSOLE DISPLAYS 9;
 }
 HEY Readers Let`s Try The Same Logic But Now K=7 .NOTE[ALWAYS K+1 IS POWER OF 2,LIKE 3,7,15,31 ETC];
 
 3)Check Whether i TH Bit Is Set Or Not?
 
 Step1:Left Shift 1 by i times.
 Step2:TAKE & With One After Left Shifting i Times .
 Let`s See The Program.
 
 int main()
 {
 int a,i;
 cin>>a>>i;
 int temp=1;
 temp=temp<<i;
 if(a&temp)cout<<"i TH BIT IS SET";
 
 else cout<<"i TH BIT IS NOT SET".
 }
5.RIGHT SHIFT OPERATOR[>>]

THE MOTTO OF RIGHT SHIFT OP IS IT SHIFTS THE BITS BY I POSITONS RIGHT.Lets see How it Works.
Let`s Take A 8 BIT Number For Simplicity And that Number Is 3.
0000001[1]
now 3>>1 Means The FIRST[FROM RIGHT] BIT goes OFF AND New ZERO WIll BE added At LEFT.
[0]0000001
NEW BIT IS ADDED AT LEFT.
Important Properties Of RIGHT SHIFT Op are.

1)RIGHT Shifting A Number i Times Then The Number Becomes N=N/pow(2,i);//predefined Pow Function.

2)If We Right Shift A Number By One It Is Same as Dividing[FLOOR DIVISION] A Number By  2.

LET`S  SEE SOME QUESTIONS ON RIGHT SHIFT OP.

1)DIVIDE A NUMBER BY 2 WITHOUT USING "*" OPERATOR.
Let`s See The Program.

int main()
{
int a=4
cin>>a;
cout<<a>>1;//CONSOLE DISPLAYS [4/2]=2;
}

2.Count Number Of Set Bits In A Number.[We Already Discussed Above ,MIN Flips to convert A TO B].
6)~ OPERATOR.

 It is An Simple operator It Flips ALL The Bits Of A Number And It Takes Only One Parameter.
 EX:~a;
HOPE U LIKED THIS POST/ARTICLE .
IF U PRACTICE AFTER READING THIS U WILL GET A SOLID KNOWLEDGE IN BIT MANIPULATION.
🔥🔥🔥
Comments (6)