{"id":2062,"date":"2016-08-13T00:01:34","date_gmt":"2016-08-13T07:01:34","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2062"},"modified":"2016-08-13T09:37:21","modified_gmt":"2016-08-13T16:37:21","slug":"the-byteflip-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2062","title":{"rendered":"The <em>byteflip()<\/em> Function"},"content":{"rendered":"<p>When I recently researched <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2046\">byte-flipping<\/a> on the Internet, I was surprised that the solution I used back in the 1980s wasn&#8217;t listed among the top solutions available today. That&#8217;s probably because my brain thinks like an Assembly Language programmer, so I crafted a low-level solution.<br \/>\n<!--more--><br \/>\nThe most overt solution I saw listed a table that simply looked up the flipped values. That works, but for my solution I opted to use a bit test and the &lt;&lt; and &gt;&gt; operators to shift bits in a byte. Here is the function I concocted in C, which echoes my earlier Assembly Language code:<\/p>\n<pre class=\"screen\">\r\nunsigned char flipbyte(unsigned char b)\r\n{\r\n    int x;\r\n    unsigned char r = 0;\r\n\r\n    for(x=0; ; x++)\r\n    {\r\n        if( b &amp;  0x80 )    <span class=\"comments\">\/* test far left bit *\/<\/span>\r\n            r |=  0x80;        <span class=\"comments\">\/* set far left bit *\/<\/span>\r\n        if( x == 7 )           <span class=\"comments\">\/* don't rotate after bit 7 *\/<\/span>\r\n            break;\r\n        b &lt;&lt;= 1;         <span class=\"comments\">\/* rotate bits *\/<\/span>\r\n        r &gt;&gt;= 1;\r\n    }\r\n    return(r);\r\n}<\/pre>\n<p>The <em>for<\/em> loop&#8217;s exit condition is found in the middle of its associated statements. At Line 33 (in the overall code), an <em>if<\/em> test determines when <code>x<\/code> is equal to 7. If so, the loop stops as all 8 bytes have been examined.<\/p>\n<p>Within the loop, <em>unsigned char<\/em> variable <code>b<\/code>&#8216;s left-most bit is tested (Line 31). If it&#8217;s set, the left-most bit in variable <code>r<\/code> is set (Line 32). Figure 1 illustrates this process:<\/p>\n<div id=\"attachment_2063\" style=\"width: 357px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2063\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0813-figure1-byte-flip.png\" alt=\"Figure 1. Testing a bit and setting a bit.\" width=\"347\" height=\"231\" class=\"size-full wp-image-2063\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0813-figure1-byte-flip.png 347w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0813-figure1-byte-flip-300x200.png 300w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><p id=\"caption-attachment-2063\" class=\"wp-caption-text\">Figure 1. Testing a bit and setting a bit.<\/p><\/div>\n<p>The next step in the loop shifts bits in each variable: <code>b<\/code> is shifted to the <em>left<\/em> one notch; <code>r<\/code> is shifted to the <em>right<\/em>. Figure 2 illustrates this process.<\/p>\n<div id=\"attachment_2064\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2064\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0813-figure2-byte-flip.png\" alt=\"Figure 1. After the bytes are shifted, left and right.\" width=\"400\" height=\"153\" class=\"size-full wp-image-2064\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0813-figure2-byte-flip.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0813-figure2-byte-flip-300x115.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-2064\" class=\"wp-caption-text\">Figure 1. After the bytes are shifted, left and right.<\/p><\/div>\n<p>In both instances, Lines 35 and 36 in the code, a zero (unset bit) is shifted in. The net effect is that bits are transferred from variable <code>b<\/code> on the left and they end up in variable <code>r<\/code> on the right.<\/p>\n<p>The loop has to bail out early because otherwise the two shift statements would alter the final results. So the final bit is set, then the function terminates and returns the flipped byte at Line 38.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0813.c\">Click here<\/a> to view the full code. Here is the sample output:<\/p>\n<pre><code>Original - Flipped\r\n00000011 = 11000000\r\n00000110 = 01100000\r\n00001100 = 00110000\r\n00011000 = 00011000\r\n00110000 = 00001100\r\n01100000 = 00000110\r\n11000000 = 00000011<\/code><\/pre>\n<p>In the code, I took a byte value that was interesting to look at, <code>0x03<\/code>, and then flipped it. The <em>main()<\/em> function has a loop that keeps shifting that value left so that you can see the progression as the bytes are flipped. The code also uses the <em>binbin()<\/em> function, which is found in my <em>Beginning Programming with C For Dummies<\/em> book.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My solution to get from 11100001 to 10000011. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2062\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2062","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2062"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2062\/revisions"}],"predecessor-version":[{"id":2084,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2062\/revisions\/2084"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}