Parity

From Oracle FAQ
Jump to: navigation, search

Parity is an error detection scheme that uses an extra checking bit, called the parity bit, to allow the receiver to verify that the data is error free.

Parity calculation[edit]

Here is a sample PL/SQL function that simulates parity calculation:

CREATE OR REPLACE FUNCTION parity(b1 CHAR, b2 CHAR, b3 CHAR, b4 CHAR,
                                  b5 CHAR, b6 CHAR, b7 CHAR, b8 CHAR)
RETURN CHAR IS
BEGIN
   RETURN utl_raw.bit_xor(utl_raw.bit_xor(utl_raw.bit_xor(utl_raw.bit_xor(
          utl_raw.bit_xor(utl_raw.bit_xor(utl_raw.bit_xor(
          b1, b2), b3), b4), b5), b6), b7), b8);
END;
/

Example[edit]

Calculating parity for 8 byte values:

SQL> SELECT parity('11111111', '10101010', '01010101', '00000000',
  2                '11001100', '00110011', '00011101', '11100010') AS Parity
  3    FROM dual;
PARITY
----------------------------------------------------------------------------
00000000

So, if any single input byte is lost, it can be recalculated with the parity byte. For example, if we lose the second byte:

SQL> SELECT parity('11111111', '00000000', '01010101', '00000000',
  2                '11001100', '00110011', '00011101', '11100010') AS Parity
  3     FROM dual;
PARITY
----------------------------------------------------------------------------
10101010

And, we have it again!

Also see[edit]

Glossary of Terms
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #