Good, Bad, So-So?
The question was, in positive multiples of 15, what bits are set to 1?
Turbo C has specific extensions for dealing with bits in a byte, but I was trying for a more standard C way of counting and displaying the set bits in an unsigned long of 4 bytes.
Suggestions encouraged.
Code:
/*
4,294,967,295 == ULONG_MAX
*/
#include <stdio.h>
//include <limits.h> //for Turbo C only
#define ULONG_MAX 4294967295 //for non Turbo C compilers
int main() {
int i, j, k, got1, cbits[33]; //cbits shows the bits, when displayed
unsigned long n;
unsigned long a[33]; //power of 2 array
printf("\n\n Sizeof(unsigned long): %d maximum unsigned long is: %lu",sizeof(unsigned long),ULONG_MAX);
n=2;
i=1;
a[i]=i;
/* build powers of 2 array for bit work */
while(n < ULONG_MAX && n > 0) {
a[++i] = n;
printf("\n n == %lu i== %d", n, i);
if(i % 22==0) {
printf("\n press enter to continue");
getchar();
}
n *= 2;
}
/* processing section */
k=0;
n=15;
while(n < ULONG_MAX) {
for(j=0;j<33;j++) //clear the display array
cbits[j]=0;
for(i=1, got1=0;i<33;i++) {
if(n & a[i]) { //AND with the mask
++got1; //got a 1 bit
cbits[i] = 1; //set the cbit index that represents the bit
}
}
if((got1 == 4 ) || (got1 > 25)) { //displays number, # of bits set, and bytes
printf("\n#%6d: N %8lu has %d 1 bits: ", ++k, n, got1);
for(j=32;j>0;j--) {
printf("%d", cbits[j]);
if((j % 8==1) ) //&& j < 32)
putchar(' ');
}
if(k%22==0) {
printf("\n hit enter to continue");
getchar();
}
}
n+=15;
}
printf("\n\n\t\t\t press enter when ready");
i = getchar();
return 0;
}