{"id":6232,"date":"2024-02-08T00:01:47","date_gmt":"2024-02-08T08:01:47","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6232"},"modified":"2024-02-03T13:41:53","modified_gmt":"2024-02-03T21:41:53","slug":"positive-negative-positive-negative-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6232","title":{"rendered":"Positive Negative Positive Negative &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6224\">month&#8217;s Exercise<\/a> presents what I often refer to as a code &#8220;toggle.&#8221; Many paths lead to a solution.<br \/>\n<!--more--><br \/>\nBecause of my interest in coding various math equations, my goal is to craft a solution distilled into a single expression. Turns out that all of my solutions easily fit into a single expression, even the one that didn&#8217;t work.<\/p>\n<p>For my first solution, I use the modulus operator in a ternary expression:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02-Exercise-a.c\" rel=\"noopener\" target=\"_blank\">2024_02-Exercise-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n\r\n    for( a=0; a&lt;14; a++ )\r\n        printf(\"%d\\n\",a%2?-1:1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>a<\/code> loops from 0 through 14. The expression in the <em>printf()<\/em> statement tests for odd and even values of <code>a<\/code>. For odd values, -1 is output; for even values 1 is output.<\/p>\n<p>For my second solution, I went binary. Bits toggle between two values, which can also be set in a single expression:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02-Exercise-b.c\" rel=\"noopener\" target=\"_blank\">2024_02-Exercise-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n\r\n    for( a=0; a&lt;14; a++ )\r\n        printf(\"%d\\n\",a&amp;0x01?-1:1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The bitwise manipulation <code>a&amp0x01<\/code> masks bits 7 through 2 in 16-bit integer value. What remains is the right-most bit, which alternates between zero and one for increasing values of variable <code>a<\/code>. The effect is the same as my first solution, resulting in TRUE\/FALSE condition in a ternary operation.<\/p>\n<p>For my last solution, I turned to the C23 standard and its variable integer width data specification, <em>_BitInt(n)<\/em>. I figured that I&#8217;d be truly clever and create a one-bit integer.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02-Exercise-c.c\" rel=\"noopener\" target=\"_blank\">2024_02-Exercise-c.c<\/a> (C23)<\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n    _BitInt(2) toggle;\r\n\r\n    toggle = 0;\r\n    for( a=0; a&lt;14; a++ )\r\n    {\r\n        printf(\"%d\\n\",(int)toggle);\r\n        toggle++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>As you can see in the source code, the <em>_BitInt(n)<\/em> declaration uses two bits, not one. Apparently the C23 standard doesn&#8217;t allow for single bit integers, at least on the <em>clang-15<\/em> compiler I&#8217;m using.<\/p>\n<p>Here is the output from my final attempt:<\/p>\n<p><code>0<br \/>\n1<br \/>\n-2<br \/>\n-1<br \/>\n0<br \/>\n1<br \/>\n-2<br \/>\n-1<br \/>\n0<br \/>\n1<br \/>\n-2<br \/>\n-1<br \/>\n0<br \/>\n1<\/code><\/p>\n<p>The values oscillate, as any integer would between its limits. The range for a 2-bit integer in C23 is from -2 through 1. Yes, it&#8217;s a signed value. I tried casting the variable to unsigned, but it didn&#8217;t work. So much for being clever. (And remember that you must use a compatible C23 compiler with the <code>-std=c2x<\/code> switch.)<\/p>\n<p>I hope you came up with something interesting or different. This type of exercise is one that yields a variety of results &mdash; perhaps even including a few obfuscated examples. This potential is why I so enjoy the C language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise presents what I often refer to as a code &#8220;toggle.&#8221; Many paths lead to a solution.<\/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-6232","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\/6232","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=6232"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6232\/revisions"}],"predecessor-version":[{"id":6247,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6232\/revisions\/6247"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}