{"id":1524,"date":"2015-09-01T00:01:06","date_gmt":"2015-09-01T07:01:06","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1524"},"modified":"2015-09-08T07:57:58","modified_gmt":"2015-09-08T14:57:58","slug":"flip-that-bit","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1524","title":{"rendered":"Flip That Bit"},"content":{"rendered":"<p>All the nonsense that takes place in programming happens because it&#8217;s possible to change the value of a bit from 0 to 1 and back again. The general concept is known as a <em>toggle switch<\/em>: The item can be set and reset between on and off positions.<br \/>\n<!--more--><br \/>\nMost C language programmers use an <em>int<\/em> variable as a toggle switch. The variable is set to 1 or 0 for TRUE\/FALSE or On\/Off &mdash; however you want to look at it.<\/p>\n<p>You can also use the <em>_Bool<\/em> variable type, providing that your compiler is C99 compatible, which in 2015 it should be.<\/p>\n<blockquote><p><em>_Bool<\/em> comes from Boolean (<em>boo&#8217;-lee-an<\/em>), which is named after British mathematician George Boole. I find it enjoyable to say the word, &#8220;Boolean.&#8221;<\/p><\/blockquote>\n<p>Using the <em>_Bool<\/em> variable type isn&#8217;t as popular as using an <em>int<\/em>, probably because most C programmers are of the grizzled, bearded, sandal-wearing, Unix variety and they grumble and grouse about the underscore keywords introduced with the C99 standard.<\/p>\n<p>Whatever.<\/p>\n<p>Here is sample code that makes used of the <em>_Bool<\/em> variable type:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    _Bool bit;\r\n\r\n    bit = 0;\r\n    printf(\"The value of _Bool 'bit' is %d\\n\",bit);\r\n    printf(\"The size of _Bool 'bit' is %lu\\n\",sizeof(bit));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>The value of _Bool 'bit' is 0\r\nThe size of _Bool 'bit' is 1<\/code><\/pre>\n<p>For this month&#8217;s exercise, you must work with an array of 10 Boolean values:<\/p>\n<pre><code>_Bool toggle[10] = {\r\n    1, 0, 1, 1, 0, 1, 1, 0, 1, 0\r\n};<\/code><\/pre>\n<p>Your code must take each Boolean value in the array and flip it, changing 1 to 0 and 0 to 1. This is a pretty basic programming operation, and I can imagine several ways to accomplish this task.<\/p>\n<p>Don&#8217;t let the <em>_Bool<\/em> variable type confuse you. The array could very well just be <em>int<\/em> values and the result would be the same.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1547\">Click here<\/a> to view my solution. As usual, please try this Exercise on your own before you peek at what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s one of the most basic processor operations. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1524\">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":[3],"tags":[],"class_list":["post-1524","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1524","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=1524"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1524\/revisions"}],"predecessor-version":[{"id":1561,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1524\/revisions\/1561"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}