{"id":1866,"date":"2016-04-16T00:01:01","date_gmt":"2016-04-16T07:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1866"},"modified":"2016-04-23T07:38:13","modified_gmt":"2016-04-23T14:38:13","slug":"bit-manipulation-program-demo-time","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1866","title":{"rendered":"Bit Manipulation Program Demo Time"},"content":{"rendered":"<p>To assemble all the bit field manipulation functions into one program, I present the status portion of what could be a complex computer game. The bit fields represent the current conditions of the game&#8217;s starship. The fields are adjusted as the game progresses and various things happen.<br \/>\n<!--more--><br \/>\nThe code is rather long, so I&#8217;m including it as a link: <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/0416.c\"rel=\"\">Click here<\/a> to view the code.<\/p>\n<p>Here is a sample run:<\/p>\n<pre><code>Stardate 41616\r\n\tTransporter is working.\r\n\tWarp engines online.\r\n\tShields up.\r\n\tPhasers available.\r\nThe Klingons are attacking!\r\n\tTransporter is working.\r\n\tWarp engines online.\r\n\tThis shields are down!\r\n\tPhasers available.<\/code><\/pre>\n<p>The program uses the <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1440\"><em>enum<\/em><\/a> keyword to establish constants for each bit position (Line 8):<\/p>\n<pre class=\"screen\">\r\nenum {\r\n    TRANSPORTER,\r\n    WARP,\r\n    SHIELDS,\r\n    PHASERS\r\n};<\/pre>\n<p>This statement appears externally, allowing these constants to be used throughout the code. That way the constants are referenced instead of bit positions, which helps make the code more readable.<\/p>\n<p>For example, the statement below activates the transporter function.<\/p>\n<pre><code>bit_set(TRANSPORTER,&status);<\/code><\/pre>\n<p>For more readability, I could rename the <em>bit_set()<\/em> function to <em>activate()<\/em>:<\/p>\n<pre><code>activate(TRANSPORTER,&status);<\/code><\/pre>\n<p>The code is clearer, but the point of this Lesson is to demonstrate the functions I&#8217;ve introduced already, so I kept the same names.<\/p>\n<p>As a review, the functions are <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1836\"><em>bit_set()<\/em><\/a>, <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1848\"><em>bit_reset()<\/em><\/a>, and <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1848\"><em>bit_test()<\/a><\/em>.<\/p>\n<p>In the program&#8217;s <em>show_status()<\/em> function (Line 34), multiple <em>if<\/em> tests display strings to confirm the current ship&#8217;s status for the user. In a real game, these items might be displayed on the screen as lights or indicators, but the purpose here is to demonstrate how bits are manipulated individually to change certain on-off items in the game.<\/p>\n<p>In my own code, I generally use an <em>int<\/em> or <em>char<\/em> variable to set the state of some TRUE or FALSE item. But when multiple on-off items need to be tracked, I revert back to individual bit fields to handle the status of the TRUE\/FALSE items. It&#8217;s probably a holdover from my early days as a microcomputer programmer, and from my love of Assembly language, where bit fields are most popular.<\/p>\n<p>And now the big news: Bit fields are often wider than a single bit. A field 2-bits wide can hold four status values. Back in the early microcomputer days, operating systems stored dates in a single byte, which was insane but that&#8217;s how it worked. Multiple bit fields of differing widths in a byte means that you must do more binary manipulation than I&#8217;ve demonstrated so far in this series of Lessons.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1874\">next week&#8217;s Lesson<\/a>, I demonstrate how to pull multi-bit values from a byte.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All the bit manipulation functions &mdash; <em>bit_set()<\/em>, <em>bit_reset()<\/em>, and <em>bit_test()<\/em> &mdash; jammed into one program. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1866\">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-1866","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\/1866","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=1866"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1866\/revisions"}],"predecessor-version":[{"id":1903,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1866\/revisions\/1903"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}