Parity flag
In computer processors the parity flag indicates if the numbers of set bits is odd or even in the binary representation of the result of the last operation. It is normally a single bit in a processor status register.
For example, assume a machine where a set parity flag indicates even parity. If the result of the last operation were 26, the parity flag would be 0 since the number of set bits is odd. Similarly, if the result were 10 then the parity flag would be 1.
Some microcontrollers, notably the ubiquitous 8051, include a parity flag to help with implementing RS-232 and other serial communication protocols, in lieu of a UART with parity support.
x86 processors
processors include a parity flag because they are descended from the Datapoint 2200 terminal, which was designed for serial communication duties.In x86 processors, the parity flag reflects the parity of only the least significant byte of the result, and is set if the number of set bits of ones is even. According to the Intel 80386 manual, the parity flag is changed in the x86 processor family by the following instructions:
- All arithmetic instructions;
- Compare instruction ;
- Logical instructions - XOR, AND, OR;
- the TEST instruction.
- Instructions which write to the entire flags register: POPF, IRET, interrupts, or any other instruction which causes a hardware task switch.
One common reason to test the parity flag is to check an unrelated x87-FPU flag. The FPU has four condition flags, but they can not be tested directly, and must instead be first copied to the flags register. When this happens, C0 is placed in the carry flag, C2 in the parity flag and C3 in the zero flag. The C2 flag is set when e.g. incomparable floating point values are compared with the FUCOM instructions.