Aloha from Honolulu,
I have a little c program for studying bit operators. I'm trying to
print out a byte in binary form. My first 'for' loop increments the
index 'i' and works fine, but the number reads backwards. I tried the
same thing but with an index that is decremented 'x--', but the loop
never executes. Can anyone help?
Here is the program:
#include <stdio.h> // for std::cout
struct bit_def {
char b0:1;
char b1:1;
char b2:1;
char b3:1;
char b4:1;
char b5:1;
char b6:1;
char b7:1;
};
union byte_def{
struct bit_def bit;
char byte;
};
union byte_def test;
char bits[8];
void init01(char ch);
int main()
{
int i,x;
init01(0x25); // Enter a byte size number to use.
printf("test.byte & test.bit. b0 to b7:\n");
printf("test.byte = 0x%X ",test.byte);
printf("\n");
for(i=0;i<8;i++)
printf("%c",bits[i]);
printf("\n");
printf("Here we are entering the decrement loop...\n");
for ( x = 8; x < 1; x-- ) // Why doesn't anything in the loop execute?
printf("%c",bits[x-1]);
printf("Here we are after the decrement loop.\n");
printf("\n");
system("PAUSE");
return 0;
}
void init01(char ch)
{
test.byte = ch;
if(test.byte & test.bit.b0) bits[0]='1';
else bits[0]='0';
if(test.byte & test.bit.b1) bits[1]='1';
else bits[1]='0';
if(test.byte & test.bit.b2) bits[2]='1';
else bits[2]='0';
if(test.byte & test.bit.b3) bits[3]='1';
else bits[3]='0';
if(test.byte & test.bit.b4) bits[4]='1';
else bits[4]='0';
if(test.byte & test.bit.b5) bits[5]='1';
else bits[5]='0';
if(test.byte & test.bit.b6) bits[6]='1';
else bits[6]='0';
if(test.byte & test.bit.b7) bits[7]='1';
else bits[7]='0';
}
Mahalo (Thank You),
Roger
|