Maverickmax
Advanced Member level 1
Bitwise problem
Hi
I need your help to understand the code:
"printf("%d", ( byte & 128 ) ? 1 : 0 );"
*********************************************************************** *
void binary_op(char byte); /* Print a byte in binary. */
/************************************************************************/
main()
{
char byte=55;
binary_op(byte); /* Print a byte in binary. */
}
/************************************************************************/
void binary_op(char byte)
{
int count=8; /* Number of bits in a byte. */
while(count--)
{
/* AND the high order bit (the
* left one) If the bit is set,
* print a ONE. */
printf("%d", ( byte & 128 ) ? 1 : 0 );
/* Move all the bits LEFT. */
byte <<= 1;
}
printf("\n");
}
Hi
I need your help to understand the code:
"printf("%d", ( byte & 128 ) ? 1 : 0 );"
*********************************************************************** *
void binary_op(char byte); /* Print a byte in binary. */
/************************************************************************/
main()
{
char byte=55;
binary_op(byte); /* Print a byte in binary. */
}
/************************************************************************/
void binary_op(char byte)
{
int count=8; /* Number of bits in a byte. */
while(count--)
{
/* AND the high order bit (the
* left one) If the bit is set,
* print a ONE. */
printf("%d", ( byte & 128 ) ? 1 : 0 );
/* Move all the bits LEFT. */
byte <<= 1;
}
printf("\n");
}