{"id":26726,"date":"2017-12-22T20:29:59","date_gmt":"2017-12-22T14:59:59","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26726"},"modified":"2017-12-22T20:29:59","modified_gmt":"2017-12-22T14:59:59","slug":"swap-two-nibbles-byte","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/swap-two-nibbles-byte\/","title":{"rendered":"C Program-Swap two nibbles in a byte"},"content":{"rendered":"<p>A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte.<br \/>\nGiven a byte, swap the two nibbles in it. For example 100 is be represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in decimal.<br \/>\n<strong>We strongly recommend that you click here and practice it, before moving on to the solution.<\/strong><br \/>\nTo swap the nibbles, we can use bitwise &amp;, bitwise \u2018&lt;&lt;&#8216; and &#8216;&gt;&gt;\u2019 operators. A byte can be represented using a unsigned char in C as size of char is 1 byte in a typical C compiler. Following is C program to swap the two nibbles in a byte.<\/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\/> <br\/>unsigned char swapNibbles(unsigned char x)<br\/>{<br\/>    return ( (x &amp; 0x0F)&lt;&lt;4 | (x &amp; 0xF0)&gt;&gt;4 );<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>    unsigned char x = 100;<br\/>    printf(&quot;%u&quot;, swapNibbles(x));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>70<\/pre>\n<p><strong>Explanation:<\/strong><br \/>\n100 is 01100100 in binary. The operation can be split mainly in two parts<br \/>\n1) The expression \u201cx &amp; 0x0F\u201d gives us last 4 bits of x. For x = 100, the result is 00000100. Using bitwise \u2018&lt;&lt;&#8216; operator, we shift the last four bits to the left 4 times and make the new last four bits as 0. The result after shift is 01000000. 2) The expression \u201cx &amp; 0xF0\u201d gives us first four bits of x. For x = 100, the result is 01100000. Using bitwise \u2018&gt;&gt;\u2019 operator, we shift the digit to the right 4 times and make the first four bits as 0. The result after shift is 00000110.<\/p>\n<p>At the end we use the bitwise OR \u2018|\u2019 operation of the two expressions explained above. The OR operator places first nibble to the end and last nibble to first. For x = 100, the value of (01000000) OR (00000110) gives the result 01000110 which is equal to 70 in decimal.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Program Swap two nibbles in a byte &#8211; Bit Algorithm &#8211; A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte.<\/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":[79738,79734,79736,79739,79742,79746,79749,79744,79745,79737,79735,79733,79743,79741,79748,79747,79740],"class_list":["post-26726","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-c-programming","category-coding","tag-bit-swapping-program-in-c","tag-byte-swap-32-bit-integer","tag-byte-swap-example","tag-nibble-swap-64-bit","tag-reverse-bits-in-a-byte-in-c","tag-reverse-individual-words-of-the-sentence","tag-reverse-individual-words-of-the-sentence-in-c","tag-swap-adjacent-bits-codefights","tag-swap-alternate-bits-of-a-given-number","tag-swap-bits-in-a-byte-in-c","tag-swap-nibbles-assembly-language","tag-swap-nibbles-in-integer","tag-swap-odd-and-even-bits-in-an-integer","tag-swap-two-bits-of-integer-in-java","tag-swap_bits-42","tag-swapping-individual-bits-with-xor","tag-swapping-two-bits-in-a-byte"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26726","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=26726"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26726\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}