#include<stdio.h>
#include<conio.h>
#include<Windows.h>
#include<math.h>
#define _BV(x) 1<<x
int checkbits(int, int, int);
int main()
{
int status;
status = checkbits(14, 3, 3);
if (status)
printf("Required bits are ON\n");
else
printf("Required bits are OFF\n");
_getch();
return 0;
}
int checkbits(int x, int p, int n)
{
unsigned int andmask = 0;
int i, j;
for (i = 0, j = p; i < n; i++, j--)
andmask |= _BV(j);
if ((x & andmask) == andmask)
return 1;
else
return 0;
}
Ad
Chapter - 15: Operations On Bits
Consider an unsigned integer in which rightmost bit is numbered as zero. Write a function checkbits(x, p, n) which returns true if all "n" bits starting from position "p" are turned on. For example, checkbits(x, 4, 3) will return true if bits 4, 3 and 2 are 1 in number x.
Ad
Ad