{"id":1547,"date":"2015-09-08T00:01:48","date_gmt":"2015-09-08T07:01:48","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1547"},"modified":"2015-09-05T10:22:09","modified_gmt":"2015-09-05T17:22:09","slug":"flip-that-bit-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1547","title":{"rendered":"Flip That Bit &#8211; Solution"},"content":{"rendered":"<p>For my solution to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1524\">this month&#8217;s Exercise<\/a> I went binary. Several of the C language operators work at a binary level. Because the exercise was to make a binary, or bitwise, modification, I figured a binary operator would be in order.<br \/>\n<!--more--><br \/>\nAs a review, here are the C language bitwise operators: &#038;, |, ^, ~, !<\/p>\n<p>These operators are discussed in Chapter 17 of my book, <em>Beginning C Programming For Dummies<\/em>. The two that may appeal to you as a potential for a solution are the one&#8217;s compliment (~) and NOT (!). Of those two, however, NOT is the one you want. That&#8217;s because at a binary level 0 equals !1 and 1 equals !0.<\/p>\n<p>Here is my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define COUNT 10\r\n\r\nint main()\r\n{\r\n    _Bool toggle[COUNT] = {\r\n        1, 0, 1, 1, 0, 1, 1, 0, 1, 0\r\n    };\r\n    int x;\r\n\r\n    for(x=0;x&lt;COUNT;x++)\r\n    {\r\n        printf(\"%d -&gt; \",toggle[x]);\r\n        toggle[x] = !toggle[x];\r\n        printf(\"%d\\n\",toggle[x]);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The Boolean array is created at Lines 7 through 9.  The <em>for<\/em> loop at Line 12 churns through the array: The <em>printf()<\/em> statement at Line 14 displays the current binary value. Line 15 flips the bit. Line 16 displays the new value.<\/p>\n<p>The output:<\/p>\n<pre><code>1 -&gt; 0\r\n0 -&gt; 1\r\n1 -&gt; 0\r\n1 -&gt; 0\r\n0 -&gt; 1\r\n1 -&gt; 0\r\n1 -&gt; 0\r\n0 -&gt; 1\r\n1 -&gt; 0\r\n0 -&gt; 1<\/code><\/pre>\n<p>Another solution, would replace Line 15 with the ternary operator:<\/p>\n<pre><code>toggle[x] = toggle[x] ? 0 : 1;<\/code><\/pre>\n<p>In this solution, the value of <code>toggle[x]<\/code> is evaluated. If TRUE (1), then 0 is generated, otherwise 1 is generated. The results are the same, but it&#8217;s not the most efficient solution nor is it in the Boolean or binary theme of the Exercise. That&#8217;s okay because I&#8217;m always happy when programmers find a way to sneak in the ternary operator.<\/p>\n<p>And, of course, other solutions are possible. Even an <em>if<\/em> decision tree works. And you can use multiple loops as well if you prefer a different presentation style.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For my solution to this month&#8217;s Exercise I went binary. Several of the C language operators work at a binary level. Because the exercise was to make a binary, or bitwise, modification, I figured a binary operator would be in &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1547\">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":[5],"tags":[],"class_list":["post-1547","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1547","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=1547"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1547\/revisions"}],"predecessor-version":[{"id":1557,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1547\/revisions\/1557"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}