C++ operator Bit wise operators
Bit wise operators perform operation at binary level.BY ”at binary level” I mean the raw bits that makes up every data(letters,image,movies,etc.) in our computer .For example consider the letter ‘B’,this letter has ASCII value as 66 and 66 in binary format is 1000010.If the letter ‘B’ is utilized as one of the operands in an expression involving bit wise operators then it’s binary format ‘1000010’ is taken to perform the operation not the value 66.
Since the operand of a bit wise operator are in binary format there can be only two operands value:1 or 0,using these value a bit wise operators perform the operation.There are six types of bit wise operators in C++:
i)AND denoted by ‘&‘.
ii)OR denoted by ‘|‘.
iii)Exclusive or denoted by ‘^‘.
iv)Left shift denoted by ‘<<‘.
v)Right shift denoted by ‘>>‘ and
vi)Bitwise complement denoted by ‘~‘.
each of them is discuss in detail below:
Note here also the operand which stands for 1 and 0 are the same as the operand for the logical operators the images below shows the type and the value it represent.
Link :Logical operators
i)AND or &
This operator like the logical ‘and’ operator also gives true only when both the operands are true which is represented by 1.So AND bit wise operator gives 1 only when both the operand bits are 1 otherwise 0.A program implementing this operator is given below.
int i1=5 , i2=15 ; int result=i1 & i2 ; cout<< result ;
The output is
To know why the output is 5,let us go down to the binary format of i1 and i2 variable and perform the operation at binary level.
The resultant binary digit of the evaluation i1&i2 is 0101 ,since result is integer type the binary digit is converted to integer value which is 5.
ii)OR or |
The bit wise ‘or’ operator gives a resultant value of 0 only when both the operands are 0,if any of the operand value is 1 then the resultant value is 1.
int i1=97, i2=156 ; int result=97|156 ; cout<< result ;
The output is
The ‘or’ operation at binary level is shown below.
iii)Exclusive or or ^
This operator works more or less like the OR operator but it gives 1 only when one of the operand value is 1.If both the operand value is 1 or 0 then the value evaluates to 0.The bit wise exclusive or performed between the value 57 and 37 is given below.
iv)Left shift or <<
Left shift operator will shift the left most bit to the left side.How many time the bit is moved depends on the value that append the operator.For instance if the expression is 29 << 3,the left most bit will move left side three times.Considering the size of 29 as 8 bits the shifting of bits is shown below.
v)Right shift or >>
Right shift operator shift the bit right side.The number of times the bit is shift is given by the value append after the operator.
If you look at the resultant value after shifting the bits ,you will notice the right shift will always decrease the value of the operand.For instance,29>>3 gives 3 whereas left shifting it gives you 232.
The left shifting increases the value however,it is not an absolute rule that Left shifting will always increase the value of the operand.For instance consider the character ‘ê’ whose ASCII value is 136,performing ‘e'<<3 will give ‘P‘ whose integer value is 80;the value is decreased.So Left shifting can either increase or decrease the value ,but right shift will always decrease the value of the operand.
vi)Bit wise complement or
This operator exchange the bit of the binary digit.
i) Shifting the number towards left ‘n’ times is same as multiplying the number by 2n,where ‘n’ is the shift count.
unsigned int n=2 ; cout << (4 << n) << endl ///gives 16 << 4*pow(2 , n) ; //give 16
The line ‘4*pow(2,n)‘ is same as 4*22
Performing multiplication by using the left shift is way faster than the method use in the second code i.e. 4*pow(2 , n).
ii) If we perform right shift the resultant value is same as performing division to the number by 2n,where ‘n’ is the shift count.But this holds true only when 2n exactly divides the number.
unsigned int n=2 ; cout << (16 >> n) << endl ///gives 4 << 16/pow(2 , n) ; /// same as 16/2(power2) ,and gives 4 cout << (26 >> n) << endl ///gives 6 << 26/pow(2 , n) ; /// gives 6.5
“16 >> n” and “16/pow(2 , n)’ gives the same value because 16 is exactly divisible by 22.But 26 is not exactly divisible by 22 ,so “26 >> n” and “26/pow(2 , n)” gives different value.