{"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[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23define%20INT_BITS%2032%0A%20%0A%2F*Function%20to%20left%20rotate%20n%20by%20d%20bits*%2F%0Aint%20leftRotate(int%20n%2C%20unsigned%20int%20d)%0A%7B%0A%20%20%20%2F*%20In%20n%3C%3Cd%2C%20last%20d%20bits%20are%200.%20To%20put%20first%203%20bits%20of%20n%20at%20%0A%20%20%20%20%20last%2C%20do%20bitwise%20or%20of%20n%3C%3Cd%20with%20n%20%3E%3E(INT_BITS%20-%20d)%20*%2F%0A%20%20%20return%20(n%20%3C%3C%20d)%7C(n%20%3E%3E%20(INT_BITS%20-%20d))%3B%0A%7D%0A%20%0A%2F*Function%20to%20right%20rotate%20n%20by%20d%20bits*%2F%0Aint%20rightRotate(int%20n%2C%20unsigned%20int%20d)%0A%7B%0A%20%20%20%2F*%20In%20n%3E%3Ed%2C%20first%20d%20bits%20are%200.%20To%20put%20last%203%20bits%20of%20at%20%0A%20%20%20%20%20first%2C%20do%20bitwise%20or%20of%20n%3E%3Ed%20with%20n%20%3C%3C(INT_BITS%20-%20d)%20*%2F%0A%20%20%20return%20(n%20%3E%3E%20d)%7C(n%20%3C%3C%20(INT_BITS%20-%20d))%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20int%20n%20%3D%2016%3B%0A%20%20int%20d%20%3D%202%3B%0A%20%20printf(%22Left%20Rotation%20of%20%25d%20by%20%25d%20is%20%22%2C%20n%2C%20d)%3B%0A%20%20printf(%22%25d%22%2C%20leftRotate(n%2C%20d))%3B%0A%20%20printf(%22%5CnRight%20Rotation%20of%20%25d%20by%20%25d%20is%20%22%2C%20n%2C%20d)%3B%0A%20%20printf(%22%25d%22%2C%20rightRotate(n%2C%20d))%3B%0A%20%20getchar()%3B%0A%7D%20\u2033 message=\u201dC programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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}]}}