void setup() {
Serial.begin(9600);
// ตั้งค่าการสื่อสารทาง Serial
}
void loop() {
// โค้ดที่ทำงานวนไปเรื่อย ๆ
byte data = 0b11001100;
// การใช้ AND บิต
byte mask1 = 0b10101010;
byte result1 = data & mask1; // ผลลัพธ์คือ 0b10001000
Serial.print("AND Result: ");
printBinary(result1);
// การใช้ OR บิต
byte mask2 = 0b10101010;
byte result2 = data | mask2; // ผลลัพธ์คือ 0b11101110
Serial.print("OR Result: ");
printBinary(result2);
// การใช้ XOR บิต
byte mask3 = 0b10101010;
byte result3 = data ^ mask3; // ผลลัพธ์คือ 0b01100110
Serial.print("XOR Result: ");
printBinary(result3);
delay(1000);
}
void printBinary(byte val) {
for (int i = 7; i >= 0; --i) {
Serial.print((val & (1 << i)) ? '1' : '0');
}
Serial.println();
}