luc112.c (988B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Rewrite the showbits() function using the _BV macro. 9 */ 10 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: B(j) */ 11 12 /* This file is auto-generated by a bot. */ 13 /* This code is not compiled; it is for reference only. */ 14 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 19 /* _BV(x) macro usually expands to (1 << x) */ 20 #define _BV(x) (1 << x) 21 22 void showbits(unsigned char n); 23 24 int main() 25 { 26 unsigned char num; 27 28 printf("Enter an 8-bit number: "); 29 scanf("%hhu", &num); 30 31 printf("Binary representation: "); 32 showbits(num); 33 printf("\n"); 34 35 return 0; 36 } 37 38 void showbits(unsigned char n) 39 { 40 int i; 41 unsigned char mask; 42 43 for (i = 7; i >= 0; i--) 44 { 45 mask = _BV(i); 46 if ((n & mask) == 0) 47 printf("0"); 48 else 49 printf("1"); 50 } 51 }