home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_11_11
/
allison
/
tbit.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-01
|
1KB
|
52 lines
LISTING 5 - Illustrates the Bit Access Functions from bit.h
/* tbit.c: Use bit operations from bit.h */
#include <stdio.h>
#include "bit.h"
main()
{
int i;
unsigned n = 0;
size_t nb = nbits(n);
/* Set the even bits */
for (i = 0; i < nb; i += 2)
n = set(n,i);
printf("n == %04X (",n);
fputb(n,stdout);
printf("), count == %d\n",count(n));
/* Toggle the upper half */
for (i = nb/2; i < nb; ++i)
n = toggle(n,i);
printf("n == %04X (",n);
fputb(n,stdout);
printf("), count == %d\n",count(n));
/* Reset the lower half */
for (i = 0; i < nb/2; ++i)
n = reset(n,i);
printf("n == %04X (",n);
fputb(n,stdout);
printf("), count == %d\n",count(n));
/* Read a bit string */
fputs("Enter a bit string: ",stderr);
n = fgetb(stdin);
printf("n == %04X (",n);
fputb(n,stdout);
printf("), count == %d\n",count(n));
return 0;
}
/* Sample Execution:
n == 5555 (0101010101010101), count == 8
n == AA55 (1010101001010101), count == 8
n == AA00 (1010101000000000), count == 4
Enter a bit string: 101001000100001
n == 5221 (0101001000100001), count == 5
/*