{"id":26125,"date":"2017-10-26T19:57:42","date_gmt":"2017-10-26T14:27:42","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26125"},"modified":"2017-10-26T19:57:42","modified_gmt":"2017-10-26T14:27:42","slug":"rotate-bits-number","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/rotate-bits-number\/","title":{"rendered":"Rotate bits of a number"},"content":{"rendered":"<p><strong>Bit Rotation:<\/strong> A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.<\/p>\n<p>In left rotation, the bits that fall off at left end are put back at right end.<\/p>\n<p>In right rotation, the bits that fall off at right end are put back at left end.<\/p>\n<p><strong>Example:<\/strong><br \/>\nLet n is stored using 8 bits. Left rotation of n = 11100101 by 3 makes n = 00101111 (Left shifted by 3 and first 3 bits are put back in last ). If n is stored using 16 bits or 32 bits then left rotation of n (000\u202611100101) becomes 00..0011100101000.<br \/>\nRight rotation of n = 11100101 by 3 makes n = 10111100 (Right shifted by 3 and last 3 bits are put back in first ) if n is stored using 8 bits. If n is stored using 16 bits or 32 bits then right rotation of n (000\u202611100101) by 3 becomes 101000..0011100.<\/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 INT_BITS 32<br\/> <br\/>\/*Function to left rotate n by d bits*\/<br\/>int leftRotate(int n, unsigned int d)<br\/>{<br\/>   \/* In n&lt;&lt;d, last d bits are 0. To put first 3 bits of n at <br\/>     last, do bitwise or of n&lt;&lt;d with n &gt;&gt;(INT_BITS - d) *\/<br\/>   return (n &lt;&lt; d)|(n &gt;&gt; (INT_BITS - d));<br\/>}<br\/> <br\/>\/*Function to right rotate n by d bits*\/<br\/>int rightRotate(int n, unsigned int d)<br\/>{<br\/>   \/* In n&gt;&gt;d, first d bits are 0. To put last 3 bits of at <br\/>     first, do bitwise or of n&gt;&gt;d with n &lt;&lt;(INT_BITS - d) *\/<br\/>   return (n &gt;&gt; d)|(n &lt;&lt; (INT_BITS - d));<br\/>}<br\/> <br\/>\/* Driver program to test above functions *\/<br\/>int main()<br\/>{<br\/>  int n = 16;<br\/>  int d = 2;<br\/>  printf(&quot;Left Rotation of %d by %d is &quot;, n, d);<br\/>  printf(&quot;%d&quot;, leftRotate(n, d));<br\/>  printf(&quot;\\nRight Rotation of %d by %d is &quot;, n, d);<br\/>  printf(&quot;%d&quot;, rightRotate(n, d));<br\/>  getchar();<br\/>} <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Rotate bits of a number &#8211; Bit Algorithm &#8211; A rotation (or circular shift)is an operation similar to shift except that the bits that fall off at one end<\/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,83615],"tags":[76283,75266,77180,75267,76280,76301,77170,75115,77181,76295,76287,76392,75282,76391,75083,75079,75088,75269,75263,75279,76304,75112,75286,75274,76292,77176,75278,75109,77172,77177,76299,76297,75106,76389,76300,76306,76290,76390,76288,76289,77175,77174,77178,77179,77169,77171,76381,77173,75293,75086],"class_list":["post-26125","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-rotate-bits","tag-and-xor","tag-binary-operation","tag-binary-operation-example","tag-binary-operator-in-c","tag-binary-operator-java","tag-binary-shift","tag-bit-calculator","tag-bit-manipulation","tag-bit-operations-java","tag-bitnot","tag-bitwise","tag-bitwise-calculator","tag-bitwise-logical-operators","tag-bitwise-operation-calculator","tag-bitwise-operations-in-c","tag-bitwise-operator-in-c","tag-bitwise-operator-in-c-example-programs","tag-bitwise-operators","tag-bitwise-operators-in-c","tag-bitwise-operators-in-c-with-examples","tag-bitwise-shift-operators-in-java-with-examples","tag-c-bit","tag-c-language-bits","tag-c-language-shift-operator","tag-c-operator","tag-c-program-for-rotation","tag-c-programming-bits","tag-c-xor","tag-circular-bit","tag-cyclic-shift","tag-flag-mask","tag-java-binary-operator","tag-java-binary-operators","tag-java-bit","tag-java-bit-shift-example","tag-java-bitwise","tag-java-bitwise-and","tag-java-shift-operator-example","tag-logical-shift","tag-operator-c-programming","tag-rotate-bits","tag-rotate-instruction-in-assembly-language","tag-rotating-shift","tag-rotation-in-c","tag-shift-calculator","tag-shift-instruction-in-assembly-language","tag-shift-operator-in-java","tag-shift-rotation","tag-what-is-bitwise-operator","tag-xor-operator-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26125","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=26125"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26125\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}