luc112.c (797B)
1 /* Rewrite the showbits() function using the _BV macro. 2 */ 3 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: B(j) */ 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 /* _BV(x) macro usually expands to (1 << x) */ 13 #define _BV(x) (1 << x) 14 15 void showbits(unsigned char n); 16 17 int main() 18 { 19 unsigned char num; 20 21 printf("Enter an 8-bit number: "); 22 scanf("%hhu", &num); 23 24 printf("Binary representation: "); 25 showbits(num); 26 printf("\n"); 27 28 return 0; 29 } 30 31 void showbits(unsigned char n) 32 { 33 int i; 34 unsigned char mask; 35 36 for (i = 7; i >= 0; i--) 37 { 38 mask = _BV(i); 39 if ((n & mask) == 0) 40 printf("0"); 41 else 42 printf("1"); 43 } 44 }