luc111.c (778B)
1 /* Receive an 8-bit number. Check if 3rd and 5th bits are OFF. If yes, put them ON. 2 */ 3 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: B(i) */ 4 5 /* This file is auto-generated by a bot. */ 6 /* This code is not compiled; it is for reference only. */ 7 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 int main() 13 { 14 unsigned char num; 15 unsigned char mask = (1 << 3) | (1 << 5); 16 17 printf("Enter an 8-bit number: "); 18 scanf("%hhu", &num); 19 20 // Check if bits are OFF. (num & mask) should be 0. 21 if ((num & mask) == 0) 22 { 23 printf("Bits 3 and 5 are OFF. Turning them ON...\n"); 24 num = num | mask; 25 } 26 else 27 { 28 printf("Bits 3 and 5 are NOT both OFF. No change.\n"); 29 } 30 31 printf("Final Value: %d (0x%02X)\n", num, num); 32 33 return 0; 34 }