bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc105.c (1224B)


      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 /* Write a function checkbits(x, p, n) which returns true if all 'n' bits starting from position 'p' are turned on.
      9 */
     10 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: B(c) */
     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 int checkbits(unsigned int x, int p, int n);
     20 
     21 int main()
     22 {
     23     unsigned int x;
     24     int p, n;
     25 
     26     printf("Enter number (x): ");
     27     scanf("%u", &x);
     28     printf("Enter starting position (p) and count (n): ");
     29     scanf("%d %d", &p, &n);
     30 
     31     if (checkbits(x, p, n))
     32         printf("TRUE: %d bits starting at %d are ON.\n", n, p);
     33     else
     34         printf("FALSE: Not all specified bits are ON.\n");
     35 
     36     return 0;
     37 }
     38 
     39 int checkbits(unsigned int x, int p, int n)
     40 {
     41     unsigned int mask;
     42     
     43     // Create a mask of n 1s. E.g., if n=3, mask=000...0111
     44     mask = (1 << n) - 1;
     45     
     46     // Shift mask to position p
     47     mask = mask << p;
     48 
     49     // Check if bits in x match the mask
     50     return (x & mask) == mask;
     51 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror