/// <summary> /// Set the most significate parity bit for supplied byte array. /// </summary> /// <param name="array">Input byte array.</param> /// <param name="parity">Parity to set.</param> /// <returns>New byte array with the correct parity.</returns> public static byte[] SetParity(byte[] array, ParityOptions parity) { if (array == null) throw new ArgumentNullException("array"); // An odd parity bit is set to 1 if the number of ones in a given set of bits is even // (making the total number of ones, including the parity bit, odd). // http://en.wikipedia.org/wiki/Parity_bit // load the bytes into one contiguous bit array structure BitArray bits = new BitArray(array); byte[] buffer = Clone(array); // two loops - the outer is to loop through the bytes // and the inner is to loop through the bits in a particular byte for (int i = 0; i < array.Length; i++) { int oneCount = 0; // loop through the bits in the byte starting with // the second bit and running to the 8th bit. // (remember that the array is zero indexed so it is off by 1) for (int j = 0; j < 7; j++) { if (bits[i * 8 + j] == true) oneCount++; } // set the parity bit (bit position 0) for the byte now that we have the one's count // if the number of one's is even then set to 1 ("true" for the BitArray) // so that now the total number of bits (8) are now odd when added up // the opposite is true for even parity switch (parity) { case ParityOptions.Odd: if (oneCount % 2 == 0) buffer[i] |= (1 << 7); else buffer[i] &= unchecked((byte)(~(1 << 7))); break; case ParityOptions.Even: if (oneCount % 2 == 0) buffer[i] &= unchecked((byte)(~(1 << 7))); else buffer[i] |= (1 << 7); break; } } return buffer; }
/// <summary> /// Set the most significate parity bit for supplied byte array. /// </summary> /// <param name="array">Input byte array.</param> /// <param name="parity">Parity to set.</param> /// <returns>New byte array with the correct parity.</returns> public static byte[] SetParity(byte[] array, ParityOptions parity) { if (array == null) { throw new ArgumentNullException("array"); } // An odd parity bit is set to 1 if the number of ones in a given set of bits is even // (making the total number of ones, including the parity bit, odd). // http://en.wikipedia.org/wiki/Parity_bit // load the bytes into one contiguous bit array structure BitArray bits = new BitArray(array); byte[] buffer = Clone(array); // two loops - the outer is to loop through the bytes // and the inner is to loop through the bits in a particular byte for (int i = 0; i < array.Length; i++) { int oneCount = 0; // loop through the bits in the byte starting with // the second bit and running to the 8th bit. // (remember that the array is zero indexed so it is off by 1) for (int j = 0; j < 7; j++) { if (bits[i * 8 + j] == true) { oneCount++; } } // set the parity bit (bit position 0) for the byte now that we have the one's count // if the number of one's is even then set to 1 ("true" for the BitArray) // so that now the total number of bits (8) are now odd when added up // the opposite is true for even parity switch (parity) { case ParityOptions.Odd: if (oneCount % 2 == 0) { buffer[i] |= (1 << 7); } else { buffer[i] &= unchecked ((byte)(~(1 << 7))); } break; case ParityOptions.Even: if (oneCount % 2 == 0) { buffer[i] &= unchecked ((byte)(~(1 << 7))); } else { buffer[i] |= (1 << 7); } break; } } return(buffer); }