{"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 &, bitwise \u2018<<\u2018 and \u2018>>\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[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%20%0Aunsigned%20char%20swapNibbles(unsigned%20char%20x)%0A%7B%0A%20%20%20%20return%20(%20(x%20%26%200x0F)%3C%3C4%20%7C%20(x%20%26%200xF0)%3E%3E4%20)%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20unsigned%20char%20x%20%3D%20100%3B%0A%20%20%20%20printf(%22%25u%22%2C%20swapNibbles(x))%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 & 0x0F\u201d gives us last 4 bits of x. For x = 100, the result is 00000100. Using bitwise \u2018<<\u2018 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 & 0xF0\u201d gives us first four bits of x. For x = 100, the result is 01100000. Using bitwise \u2018>>\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=\u201dbanner\u201d]\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}]}}