1

I have following function to print bits:

void printBits(unsigned int num)
{
   for(int bit=0;bit<(sizeof(unsigned int) * 8); bit++)
   {
      printf("%i ", num & 0x01);
      num = num >> 1;
   }
}

and I want to set bit number 5 as follow:

unsigned int DataInMemory = 0x00000000;
DataInMemory |= (1<<5);

the result of printBites function is:

0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

it means that 1 << 5 set bit 6 not bit 5. Is it difference between bit position (indexes) and bit in programming nomenclature? How to set bit and print all bits in number using C?

ElConrado
  • 1,477
  • 4
  • 20
  • 46

0 Answers0