Bitwise operators operate on bits (that is, operate on the binary value of the operand)
Operator |
Description |
##& | Bitwise AND |
##|
Bitwise OR |
|
^
Bitwise XOR |
|
Shift left |
| >>
Shift right |
|
-
Complement code |
|
##Bitwise and |
a
b | a & b |
| 0
0 | 0 |
| 0
1 | 0 |
| 1
0 | 0 |
| 1
1 | 1 |
|
##Bitwise or
a
b
a | b |
## 0 |
| 0
0 |
| 0 | 1
1 |
| 1 | 0
1 |
##1 |
1 | tr>1
|
|
|
Bitwise XOR
##a | b
a^b
| 0 | 0 | 0
| 0 | 11 |
| 1 | 0 | 1
| 11 | 0
|
|
##Example |
The following is the C Program for Addition and Multiplication 2 using Bitwise Operators -
Live Demonstration
#include<stdio.h>
main(){
int a;
printf("Enter a</p><p>");
scanf("%d",&a);
printf("%d*2=%d </p><p>",a,a<<1);
printf("%d/2=%d </p><p>",a,a>>1);
}
Output
When the above program is executed, the following output is produced -
Run 1:
Enter a
45
45*2=90
45/2=22
Run 2:
Enter a
65
65*2=130
65/2=32
The above is the detailed content of C program for addition and multiplication using bitwise operations. For more information, please follow other related articles on the PHP Chinese website!