ตัวดำเนินการบิตวายส์ใช้ทำงานระดับบิต (bit-level) บนตัวแปรสองตัว
| ตัวดำเนินการ | ชื่อ |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise Complement (NOT) |
| << | Shift Left |
| >> | Shift Right |
ตัวอย่าง
#include <stdio.h>
int main() {
int x = 10;
int y = 28;
int i = 0;
printf("Bitwise AND : %d\n", x & y);
printf("Bitwise OR : %d\n", x | y);
printf("Bitwise XOR : %d\n", x ^ y);
printf("Bitwise Complement : %d,%d\n", ~x, ~y);
for (i = 0; i < 2; i++)
printf("Right shift by %d: %d\n", i, x >> i);
for (i = 2; i <= 3; ++i)
printf("Left shift by %d: %d\n", i, y << i);
return 0;
}ผลลัพธ์
Bitwise AND : 8 Bitwise OR : 30 Bitwise XOR : 22 Bitwise Complement : -11, -29 Right shift by 0: 10 Right shift by 1: 5 Left shift by 2: 112 Left shift by 3: 224