BITWISE operator
----------------"BITWISE operator" is used to perform the operations on the "BITS".
What is the BITS?
"BITS" are 1 and 0's.
What is another name of "BITS"?
-> Another name of "BITS" is "binary language".
-> Another name of "BINARY LANGUAGE" is "MACHINE LANGUAGE".
The following are the "BITWISE OPERATORS" in C-Language
1) & - AND BITWISE OPETATOR
2) | - OR BITWISE OPERATOR
3) ^ - XOR BITWISE OPERATOR
NOTE: XOR stands for "EXCLUSIVE OR"
4) >> - RIGHT SHIFT BITWISE OPERATOR
5) << - LEFT SHIFT BITWISE OPERATOR
6) ~ - ONE'S COMPLIMENT BITWISE OPERATOR
BITWISE OPERATORS
-----------------
1) & - AND BITWISE OPERATOR
-----------------------
& - AND BITWISE OPERATOR is used to perform "AND Logical operations on BITS".
What are the "AND logical operations on BITS"?
-> NOTE: True -1 False - 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
0 & 0 = 0
Lets say, I have one integer value: 10
10 -> what are the binary bits of 10?
decimal Binary
10 = 1010
20 = 10100
WE collect reminder, lets calculate the bit value of 20.
Here, we divide 20 by 2 and we note the reminder.
2|20
2|10 = 0 reminder
2|5 = 0
2|2 = 1
1 = 0
so, you put together from bottom, we have
10100
As we know,
1 byte = 8 bits
10 = 1010 -> 4 bits
20 = 10100 -> 5 bits
so, 10 = 0 0 0 0 1 0 1 0 -> we just add 4 0's for 10 to make 8 bits which is 1 byte
20 = 0 0 0 1 1 0 1 0 0 -> we have to add 3 0's
Now, lets apply AND bitwise operator
to make a bits, we are adding 0's. As we know one byte equal to 8 bits.
Now, lets apply AND bitwise operator
10 = 0 0 0 0 1 0 1 0
20 = 0 0 0 1 0 1 0 0
---------------------
0 0 0 0 0 0 0 0
Now calculate,
2^0 -> 2 power 0 is equal to 0.
Here is an example why its 0.
0 0 0 0 0 0 0 0
(2^7*0) (2^6*0) (2^5*0) (2^4*0) (2^3*0) (2^2*0) + (2^1*0) + (0^0*0) = 0 result
When we apply 'AND bitwise operator' on 10 and 20 numbers, then what is the result?
-> 0 is the result.
void main()
{
printf("%d", 10 & 20);
}
2) | - OR BITWISE OPERATOR
--------------------------
OR BITWISE OPERATOR is used to perform OR logical operations on BITS.
What are the OR LOGICAL OPERATIONS on BITS?
->
1 | 0 = 1 True Or False -> True
0 | 1 = 1 True
1 | 1 = 1 True
0 | 0 = 0 False | False
Applying OR BITWISE Operator
10 = 0 0 0 0 1 0 1 0
20 = 0 0 0 1 0 1 0 0
-----------------------------
0 0 0 1 1 1 1 0
now, calculate from roght to left
(2^7*0) (2^6*0) (2^5*0) (2^4*1) (2^3*1) (2^2*1) (2^1*1) (2^0*0)
---------------------------------------------------------------
0 + 0 + 0 + 16 + 8 + 4 + 2 + 0 = 30 Result
So, if we apply 'OR bitwise operator on 10 and 20 numbers, what is the result?
The answer is 30.
void main()
{
printf("%d", 10 | 20);
}
3) XOR BITWISE OPERATOR
========================
BITWISE OPERATORS are used for performing calculations on bits
Here, XOR stands for EXCLUSIVE OR
XOR operations on BITS:
------------------------
It is little bit different from OR BITWISE operator.
XOR - ^
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
1 ^ 1 = 0
Q. What is the difference between 'OR' and 'XOR' bitwise operator?
->
1 | 1 = 1
1 ^ 1 = 0
lets work on XOR bitwise operator.
Applying XOR BITWISE Operator
10 = 0 0 0 0 1 0 1 0
20 = 0 0 0 1 0 1 0 0
-----------------------------
0 0 0 1 1 1 1 0
Since 1 on 10 and 1 on 20 do not fall on same order, that is why the result on 'OR' or 'XOR' comes same.
Result will be same: 30
Now, lets write the code
#include<stdio.h>
void main()
{
clrscr();
printf("%d", 10 ^ 20);
}
4) >> - RIGHTSHIFT BITWISE Operator
===================================
>> - two greater than signals.
No comments:
Post a Comment