{"id":1836,"date":"2016-04-02T00:01:27","date_gmt":"2016-04-02T07:01:27","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1836"},"modified":"2018-02-22T13:15:30","modified_gmt":"2018-02-22T21:15:30","slug":"bit-field-manipulation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1836","title":{"rendered":"Bit Field Manipulation"},"content":{"rendered":"<p>The three basic bit manipulation operations are:<\/p>\n<ul>\n<li>Set a bit (change its value to 1)<\/li>\n<li>Reset a bit (change its value to 0)<\/li>\n<li>Read a bit (determine whether it&#8217;s 1 or 0)<\/li>\n<\/ul>\n<p>The standard C library lacks specific functions to carry out these bit manipulations, and I haven&#8217;t checked to see whether a third party library is available. That&#8217;s because you can easily code these operations on your own.<br \/>\n<!--more--><br \/>\nTo manipulate a bit within a byte, word, or long word, you employ the C language&#8217;s bitwise operators: <code>&<\/code> (and), <code>|<\/code> (or), <code>^<\/code> ( exclusive or), <code>~<\/code> (one&#8217;s compliment), and <code>!<\/code> (not). Also coming in handy are the shift operators, <code>&lt;&lt;<\/code> and <code>&gt;&gt;<\/code>.<\/p>\n<p>Bitwise <code>&<\/code> and <code>|<\/code> share names and behaviors with their logical comparison counterparts, <code>&amp;&amp;<\/code> and <code>||<\/code>. The bitwise operators work at a binary level, however, which can be confusing. It helps to visualize how they work, but in a nutshell the <code>|<\/code> (or) operator can set bits and the <code>&amp;<\/code> (and) operator can reset bits. The shift operators <code>&lt;&lt;<\/code> and <code>&gt;&gt;<\/code> position bits within a value.<\/p>\n<p>Of the three bit manipulation operations mentioned at the start of this post, the most basic is to set a bit. It requires the least amount of work. All you need to know is the bit number (its offset) and then perform a bitwise <code>|<\/code> (or) operation on that specific bit. The result is that the bit is set, as illustrated in Figure 1.<\/p>\n<div id=\"attachment_1837\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1837\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure1-bit_set.png\" alt=\"Figure 1. The bitwise | (or) operator sets a specific bit in a byte.\" width=\"450\" height=\"368\" class=\"size-full wp-image-1837\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure1-bit_set.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure1-bit_set-300x245.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure1-bit_set-367x300.png 367w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1837\" class=\"wp-caption-text\">Figure 1. The bitwise <code>|<\/code> (or) operator sets a specific bit in a byte.<\/p><\/div>\n<p>Pay attention to bit 2 in Figure 1. (It&#8217;s the third bit from the right.) The bitwise <code>|<\/code> (or) operation sets that bit. This operation holds true whether the original value of bit 2 is 1 or 0; the result always sets the bit to 1.<\/p>\n<p>Here is a function to set a specific bit:<\/p>\n<pre class=\"screen\">\r\nvoid bit_set(char bit, char *byte)\r\n{\r\n    bit = 1 &lt;&lt; bit;\r\n    *byte = *byte | bit;\r\n}<\/pre>\n<p>Variable <code>bit<\/code> is the bit position, starting with 0 for the right-most bit. Variable <code>byte<\/code> is a pointer to the <em>char<\/em> variable manipulated. It&#8217;s a pointer so that a value doesn&#8217;t need to be returned from the function; the function manipulates the data directly.<\/p>\n<p>The first line in the function sets the bit position. The value 1 (<code>000000001<\/code>) is shifted left <code>&lt;&lt;<\/code> a given number of positions. When <code>bit<\/code> is zero, it&#8217;s not shifted. Otherwise, it ends up in the proper bit position (refer to Figure 1).<\/p>\n<p>As an example, if the value of <code>bit<\/code> is 2, then the <code>&lt;&lt;<\/code> operator positions the 1 at bit position 2, third from the right: <code>00000100<\/code>.<\/p>\n<p>The second line performs the bitwise <code>|<\/code> (or) operation, setting the bit at the proper position. Because <code>byte<\/code> is a pointer, the original value is manipulated and nothing needs to be returned.<\/p>\n<pre><code>bit_set(2,&status);<\/code><\/pre>\n<p>In the above statement, the second bit of variable <code>status<\/code> is set. If <code>status<\/code> equals <code>00000000<\/code>, then after the call it equals <code>00000100<\/code>.<\/p>\n<p>As a more realistic example, suppose in your computer space game, the user desires to activate the starship&#8217;s shields. Within the code, you need to set the proper bit within the <code>ships_status<\/code> bit field variable. The bit&#8217;s position is defined as the constant <code>SHIELDS<\/code>:<\/p>\n<pre><code>bit_set(SHIELDS,&ships_status);<\/code><\/pre>\n<p>Elsewhere in the code, another function, <em>bit_test()<\/em>, might evaluate that condition. Yet another function, <em>bit_reset()<\/em> might turn it off, say if the Klingons brutally and stealthily attack. I&#8217;ll cover those functions in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1848\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The delicate art of setting, resetting, and reading individual bits. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1836\">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-1836","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\/1836","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=1836"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1836\/revisions"}],"predecessor-version":[{"id":2987,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1836\/revisions\/2987"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}