{"id":26315,"date":"2017-10-26T21:00:10","date_gmt":"2017-10-26T15:30:10","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26315"},"modified":"2017-10-26T21:00:10","modified_gmt":"2017-10-26T15:30:10","slug":"write-c-program-find-parity-unsigned-integer","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-c-program-find-parity-unsigned-integer\/","title":{"rendered":"Write a C program to find the parity of an unsigned integer"},"content":{"rendered":"<p><strong>Parity:<\/strong> Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has \u201codd parity\u201d, if it contains odd number of 1-bits and is \u201ceven parity\u201d if it contains even number of 1-bits.<br \/>\nMain idea of the below solution is \u2013 Loop while n is not 0 and in loop unset one of the set bits and invert parity.<\/p>\n<pre>Algorithm: getParity(n)\r\n1. Initialize parity = 0\r\n2. Loop while n != 0      \r\n      a. Invert parity \r\n             parity = !parity\r\n      b. Unset rightmost set bit\r\n             n = n &amp; (n-1)\r\n3. return parity\r\n\r\n<strong>Example:<\/strong>\r\n Initialize: n = 13 (1101)   parity = 0\r\n\r\nn = 13 &amp; 12  = 12 (1100)   parity = 1\r\nn = 12 &amp; 11 = 8  (1000)   parity = 0\r\nn = 8 &amp; 7 = 0  (0000)    parity = 1<\/pre>\n<p><strong>Program:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\"># include &lt;stdio.h&gt;<br\/># define  bool int<br\/> <br\/>\/* Function to get parity of number n. It returns 1<br\/>   if n has odd parity, and returns 0 if n has even<br\/>   parity *\/<br\/>bool getParity(unsigned int n)<br\/>{<br\/>    bool parity = 0;<br\/>    while (n)<br\/>    {<br\/>        parity = !parity;<br\/>        n      = n &amp; (n - 1);<br\/>    }        <br\/>    return parity;<br\/>}<br\/> <br\/>\/* Driver program to test getParity() *\/<br\/>int main()<br\/>{<br\/>    unsigned int n = 7;<br\/>    printf(&quot;Parity of no %d = %s&quot;,  n, <br\/>             (getParity(n)? &quot;odd&quot;: &quot;even&quot;));<br\/>     <br\/>    getchar();<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Above solution can be optimized by using lookup table. Please refer to Bit Twiddle Hacks[1st reference] for details.<\/p>\n<p><strong>Time Complexity:<\/strong> The time taken by above algorithm is proportional to the number of bits set. Worst case complexity is O(Logn).<\/p>\n<p><strong>Uses:<\/strong> Parity is used in error detection and cryptography.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Write a C program to find parity of an unsigned integer &#8211; Bit Algorithm &#8211; Parity of a number refers to whether it contains an odd or even number of 1-bit<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852,69866,1],"tags":[78310,75880,75079,75879,78317,75078,78307,78309,70865,78308,75570,75571,78337,78348,78320,78325,78340,78336,78311,78328,78329,78335,78338,78326,78347,78344,78322,78313,78342,78324,78314,78315,78346,78323,78341,78316,78343,78319,78318,78333,78327,78312,78332,78345,78334,78330,78331,78349,78321,78339],"class_list":["post-26315","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-c-programming","category-coding","tag-8085-programs","tag-bit","tag-bitwise-operator-in-c","tag-bs-it","tag-can-to-rs232","tag-clearbit","tag-crc-code-in-c","tag-crc-program-in-c","tag-even-odd-program-in-c","tag-hamming-code-in-c","tag-odd-even-program-in-c","tag-odd-or-even-program-in-c","tag-raspberry-pi-2-serial-port","tag-raspberry-pi-serial","tag-rs-232-interface-standard","tag-rs-232c-serial","tag-rs232","tag-rs232-25-pin-to-9-pin","tag-rs232-9-pin","tag-rs232-9-pin-connector","tag-rs232-cable-specification","tag-rs232-cable-wiring","tag-rs232-connector","tag-rs232-converter","tag-rs232-devices","tag-rs232-interface","tag-rs232-modem","tag-rs232-null-modem-pinout","tag-rs232-port","tag-rs232-protocol","tag-rs232-rs422","tag-rs232-specification","tag-rs232-standard","tag-rs232-voltage","tag-rs232-voltage-levels","tag-rs32-port","tag-serial-cable-rs232","tag-serial-communication","tag-serial-communication-port","tag-serial-communication-rs232","tag-serial-data","tag-serial-device","tag-serial-interface-rs232","tag-serial-port","tag-serial-port-rs232","tag-serial-read","tag-serial-rs232","tag-serial-to-rs232-adapter","tag-uart-port","tag-uart-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26315"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26315\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}