Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>What are parity bits?</strong></p> <p>Parity bits are a simple form of error-checking that can be used, for example, when you transmit data from one machine to another. If both machines agree on, say, even parity, then the receiver will know that any incoming characters that <em>don't</em> have even parity were received in error. However, parity of any type can only identify an <em>odd</em> number of erroneous bits: two errors in a single character will cancel each other, and the resulting parity will be correct. Moreover, the receiver cannot determine <em>which</em> bit (or bits) are incorrect; parity provides error <em>detection</em>, but not error <em>correction</em>.</p> <p><strong>How should received data be processed?</strong></p> <p>The receiver should calculate and validate the parity of each incoming character. If it detects a character with invalid parity, it should indicate the error somehow. In the best case, it may be able to ask the transmitter to re-transmit the character.</p> <p>Once the character is validated, the receiver must <em>strip the parity bit</em> before passing the character on for further processing. This is because, since the parity bit is used for error detection, it is "lost" from the data payload. Thus, enabling parity will reduce the number of available data values by half. For example, 8 bits can have one of 256 possible values (0 - 255). If one bit is used for parity, only 7 bits remain to encode the data, leaving only 128 valid values.</p> <hr> <p>Since you asked for comments/criticism, here's a revised, commented version of your code:</p> <pre><code>#include &lt;stdio.h&gt; // Consider using a boolean data type, as the function returns // "true" or "false" rather than an arbitrary integer. int isOddParity(unsigned char myChar); int main(void) { unsigned char input; printf("Enter char: "); scanf("%c", &amp;input); // Force even parity by setting MSB if the parity is odd. unsigned char even = isOddParity(input) ? input | 0x80 : input; // Print the original char, original hex, and even-parity hex // Print hex values as 0xHH, zero-padding if necessary. This program // will probably never print hex values less than 0x10, but zero-padding // is good practice. printf("orig char: %c\n", input); printf("orig hex: 0x%02x\n", input); printf("even hex: 0x%02x\n", even); return 0; } // Calculate the parity of myChar. // Returns 1 if odd, 0 if even. int isOddParity(unsigned char myChar) { int parity = 0; // Clear the parity bit from myChar, then calculate the parity of // the remaining bits, starting with the rightmost, by toggling // the parity for each '1' bit. for (myChar &amp;= ~0x80; myChar != 0; myChar &gt;&gt;= 1) { parity ^= (myChar &amp; 1); // Toggle parity on each '1' bit. } return parity; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload