Bitwise Logical Operators in C

Bitwise logical Operators are the operators which are used to do Bitwise logical operations like AND , OR and NOT. The Bitwise operation is performed on the bits of the operands rather than the values. When we apply these operations on the operands the operations are perfomed on the binary equivalent of the operands.
& is used for AND
| is used for OR
~ is used for NOT

& (AND) Operator

& operator is used to do AND operation on two operands. Those operands may be integer , float or double datatypes. If we want to do AND operation on number ie 10 and 13 then we write 10 & 13. Similarly if we want to add values of a variable then we use a & b where a and b are the operands of type integer , float or double


result = 10 & 13 // result is 23
result = a & b // the result is equal to values of a + b

| OR Operator

| OR operator is used to do OR operatation on two operands . Those operands may be integer , float or double data types. If we want to OR operation on numbers ie 10 and 13 then we write 10 | 13. Similarly if we want to do OR operation on the values of a variable then we use a - b where a and b are the operands of type integer , float or double

result = 10 | 13 // result is -3
result = a | b // the result is equal to values is difference of a , b

! (NOT) Operator

NOT (!) operator is used to do not operation on the operand. This operation negates the bits of the operands. It means Bit 0 is converted to 1 and Bit 1 is converted to 0.

result = !(10) // result is 230
result = !(a) // the result is equal to product of a , b

<< Left Shift Operator

/ operator is used to do division on two operands quotient is the result. Those operands may be integer , float or double datatypes. If we want to divide to number ie 10 and 13 then we write 10 / 13. Similarly if we want to add values of a variable then we use a / b where a and b are the operands of type integer , float or double

result = 10 <<1 // result is 0.1
result = a << b // the result is equal to quotient of a / b

>> Right Shift Operator

% opeator is used to find reminder after integer division of two operands. Those operands must be integers only. If we want to add to number ie 10 and 3 then we write 10 % 3. Similarly if we want to find reminder of a variables then we use a % b where a and b are the operands of type integer.

result = 10 >> 1 // result is 1
result = a >> b // the result is equal to reminder of a / b

Bitwise Operators Demo Program



Bitwise Operators Demo Program Output