WR Home      Topic Home      Chapter:  1  2  3 
<Previous Next>

Chapter 03

Commonly used bit-manipulation functions

Page 1



It is a common reqiurment to visualize bits in a byte:

Write a C-program and a function to see the bit sequence of a byte.







The solution is given below














Incorrect Solution

The correct solutions are:
void showbits(unsigned char byte)
{
  int bit_number;

  for(bit_number = 7; bit_number >=0; bit_number--)
	{
      if(byte & (1 << bit_number) )
          printf("1");
      else
          printf("0");
    
     }

}

A N D

void showbits(unsigned char byte)
{
  int bit_number;

  for(bit_number = 7; bit_number >=0; bit_number--)
	{
      printf("%d", (byte & (1 << bit_number) ) >> bit_number);
  
  
     }



}




WR Home      Topic Home      Chapter:  1  2  3 
<Previous Next>