Bitwise operators ทำงานบนบิต (ค่าบाइनนารีของ operand)
| Operator | คำอธิบาย |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| << | Left Shift |
| >> | Right Shift |
| ~ | One's complement |
ตารางความจริงของ AND, OR, XOR (ไม่ได้รวมตาราง)
ตัวอย่างโค้ด
บวกและคูณ 2 ด้วย bitwise operators:
#include <stdio.h>
int main() {
int a;
printf("Enter a\n");
scanf("%d", &a);
printf("%d*2=%d \n", a, a << 1);
printf("%d/2=%d \n", a, a >> 1);
return 0;
}ผลลัพธ์
Run 1: Enter a 45 45*2=90 45/2=22 Run 2: Enter a 65 65*2=130 65/2=32
หมายเหตุ: Left shift (<<) คูณ 2, Right shift (>>) หาร 2 (ปัดทศนิยม)