What is Bitwise operators in C
Bitwise operators
Q) Explain bitwise operators in C.
- C has the distinction of supporting special operators knows as bitwise operators for manipulation of data at the bit level. These operators are used for testing the bits or shifting them right of left. Bitwise operators may non be applied into float either double.
![]() |
Image Source ~ Crafted With ©Ishwaranand - 2020 ~ Image by ©Ishwaranand |
Operator | Meaning |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
<< | Left shift |
>> | Right shift |
& (Bitwise AND):
- & is knows as the bitwise AND. This operator takes two operands. It applies AND operation on each bit of value.
Example:
void main()
{
int a= 10, b=12, c;
c=a&, b;
printf ("%d", c);
}
Output:
c=8
a --- 1 0 1 0
&
b --- 1 1 0 0
-----------------
c --- 1 0 0 0
| (Bitwise OR):
- | is known as the Bitwise OR. This operator takes two operands. It performs OR operation on each bit of the value.
Example:
void main()
{
int a=10, b=12, c;
/ c=a/ b;
printf ("%d", c);
}
Output:
c=14
a --- 1 0 1 0
|
b --- 1 1 0 0
-----------------
c --- 1 1 1 0
^ (Bitwise exclusion OR):
- ^ is known as the Bitwise exclusive OR. This operator takes operads. It performs exclusive OR operation on each bit of values.
Example:
void main()
{
int a=10, b=12, c;
c=a / b;
printf ("%d", c);
}
Output
c = 6
a --- 1 0 1 0
^
b --- 1 1 0 0
-----------------
c --- 0 1 1 0
Sizeof Operator
- In our program, at any instance, we may want to find the bytes occupied by the operand.
- To do this, C provides the operator called sizeof, " sizeof is the operator which is used to return the number of bytes occupied by the operand."
The operand may be variable, constant of a data type.
Example:
int a, arr[10];
float f, ff[10];
printf("%d %d %d %d", sizeof(arr), sizeof(f), sizeof(ff), sizeof(chart):
Output:
2 20 4 40 1