What is Bitwise operators in C
Bitwise operators
Q) Explain bitwise operators in C.
- C has the distinction of supporting special operators known as bitwise operators for the manipulation of data at the bit level. These operators are used for testing the bits or shifting them right or left. Bitwise operators may not be applied to 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 known 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 a 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
Last updated on Sunday - May 21st, 2023