{"id":26099,"date":"2017-10-26T19:48:26","date_gmt":"2017-10-26T14:18:26","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26099"},"modified":"2017-10-26T19:48:26","modified_gmt":"2017-10-26T14:18:26","slug":"compute-modulus-division-power-2-number","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/compute-modulus-division-power-2-number\/","title":{"rendered":"C Programming &#8211; Compute modulus division by a power of 2 number"},"content":{"rendered":"<p>Compute n modulo d without division(\/) and modulo(%) operators, where d is a power of 2 number.<\/p>\n<p>Let ith bit from right is set in d. For getting n modulus d, we just need to return 0 to i-1 (from right) bits of n as they are and other bits as 0.<\/p>\n<p>For example if n = 6 (00..110) and d = 4(00..100). Last set bit in d is at position 3 (from right side). So we need to return last two bits of n as they are and other bits as 0, i.e., 00..010.<\/p>\n<p>Now doing it is so easy, guess it\u2026.<\/p>\n<p>Yes, you have guessing it right. See the below program.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/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\/> <br\/>\/* This function will return n % d.<br\/>   d must be one of: 1, 2, 4, 8, 16, 32, \u2026 *\/<br\/>unsigned int getModulo(unsigned int n, unsigned int d)<br\/>{<br\/>  return ( n &amp; (d-1) );<br\/>}         <br\/> <br\/>\/* Driver program to test above function *\/<br\/>int main()<br\/>{<br\/>  unsigned int n = 6;<br\/>  unsigned int d = 4; \/*d must be a power of 2*\/<br\/>  printf(&quot;%u moduo %u is %u&quot;, n, d, getModulo(n, d));<br\/> <br\/>  getchar();<br\/>  return 0;<br\/>} <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Compute modulus division by a power of 2 number &#8211; Bit Algorithm &#8211; operators, where d is a power of 2 number.Let it bit from right is set in d. For getting n<\/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":[76096,75102,75584,76858,76882,76868,76086,76098,76859,75431,76878,76855,76097,76089,76088,75427,76881,76873,76879,76866,76861,76871,76099,76874,76867,76875,76870,76869,76864,76090,76856,75439,76087,76860,76863,75883,76865,76854,76876,76883,76857],"class_list":["post-26099","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-c-programming","category-coding","tag-9-mod-16","tag-bit-number","tag-c-programming-modulus","tag-function-math","tag-how-to-calculate-2-to-the-power-of-n","tag-how-to-calculate-log-base-2","tag-how-to-calculate-mod","tag-large-number-modulo-calculator","tag-math-functions","tag-math-operations","tag-mod-1-mod-2","tag-mod-calculation-example","tag-mod-computer","tag-mod-function","tag-mod-in-math","tag-mod-math","tag-mod-math-calculator","tag-mod-one","tag-mod-square","tag-modm","tag-modn","tag-modular-arithmetic","tag-modular-arithmetic-calculator","tag-modular-arithmetic-example-problems","tag-modular-arithmetic-examples","tag-modular-arithmetic-examples-and-solutions","tag-modular-arithmetic-problems","tag-modular-arithmetic-rules","tag-modulo","tag-modulo-calculator","tag-modulus","tag-modulus-operator","tag-modulus-operator-in-c","tag-modulus-x-1","tag-multiplication-modulo","tag-powers-of-2","tag-remainder-calculator","tag-sql-mod","tag-x-mod-1","tag-x-mod-2","tag-xmod"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26099","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=26099"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26099\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}