Question

I see that in x86 CPUs, the parity flag (PF) is set when the number of bits set to 1 is even, and that only the first byte (lower 8 bits) of a value are ever tested.

The only case I am not sure about is when we are dealing with a value of 0.

I have seen at least other question where the parity flag seems to be set to 1 for a value of 0.

For instance, for the value 8000h the lower 8 bits are all 0, and the parity flag is said to be set to 1.

So, shall I accept that for 0 bits set to 1, the parity flag gets enabled, just like for an even number of bits set to 1?

Was it helpful?

Solution

0 has an even number of bits, so, the answer is yes.

Test:

// compiled with Open Watcom C/C++ 1.9
#include <stdio.h>

unsigned parity(unsigned v)
{
  unsigned p = 0;
  __asm
  {
    mov eax, v
    or  eax, eax
    pushf
    pop eax
    shr eax, 2
    and eax, 1
    mov p, eax
  }
  return p;
}

int main(void)
{
  unsigned i;
  for (i = 0; i < 8; i++)
    printf("PF(%u) = %u\n", i, parity(i));
  return 0;
}

Output:

PF(0) = 1
PF(1) = 0
PF(2) = 0
PF(3) = 1
PF(4) = 0
PF(5) = 1
PF(6) = 1
PF(7) = 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top