{"id":1848,"date":"2016-04-09T00:01:15","date_gmt":"2016-04-09T07:01:15","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1848"},"modified":"2016-04-16T07:41:48","modified_gmt":"2016-04-16T14:41:48","slug":"reading-bits-and-resetting-bits","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1848","title":{"rendered":"Reading Bits and Resetting Bits"},"content":{"rendered":"<p>Once you break out the tools, bit manipulation in C can be a fascinating and useful thing. Setting a bit, covered in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1836\">last week&#8217;s Lesson<\/a>, is a big first step. Once you&#8217;ve done that, the functions to read and set a bit fall into place naturally.<br \/>\n<!--more--><br \/>\nThe goal in each function is to access a specific bit. From last week&#8217;s code snippet, the following statement is used:<\/p>\n<pre><code>bit = 1 &lt;&lt; bit;<\/code><\/pre>\n<p>Variable <code>bit<\/code> represents the bit position in a byte, word, or long word. Position 0 is at the far right. Position 1 comes next, and so on. The function sets bit 0 to the value 1. The left shift operator, <code>&lt;&lt;<\/code>, shifts that value left to the proper bit position.<\/p>\n<p>To read a bit at a specific position, you must mask out all other bits in the value. The operator that assists in that process is the bitwise <code>&<\/code> (and). After you mask out all the other bits, the value that remains is either zero or some other value. These two conditions evaluate to FALSE and TRUE in the C language, which tells you whether or not the bit was set.<\/p>\n<p>In Figure 1, you see binary value 1 shifted twice to bit position 2, as in <code>bit = 1 &lt;&lt; bit;<\/code> where <code>bit<\/code> equals 2.<\/p>\n<div id=\"attachment_1849\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1849\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure1-bit_shift_left.png\" alt=\"Figure 1. Variable bit is set to binary position 2.\" width=\"450\" height=\"270\" class=\"size-full wp-image-1849\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure1-bit_shift_left.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure1-bit_shift_left-300x180.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1849\" class=\"wp-caption-text\">Figure 1. Variable <code>bit<\/code> is set to binary position 2.<\/p><\/div>\n<p>Once the bit is positioned, an <code>&amp;<\/code> (and) mask eliminates all other bits but position 2 in the original value. The result is either 0 or 4 depending on whether the bit was off or on, respectively. Figure 2 illustrates how this operation works.<\/p>\n<div id=\"attachment_1850\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1850\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure2-and_mask.png\" alt=\"Figure 2. How an &amp; (and) mask works.\" width=\"450\" height=\"178\" class=\"size-full wp-image-1850\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure2-and_mask.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/03\/figure2-and_mask-300x119.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1850\" class=\"wp-caption-text\">Figure 2. How an <code>&<\/code> (and) mask works.<\/p><\/div>\n<p>The function I wrote to read bit status is called <em>bit_test()<\/em>. It uses the same arguments as <em>bit_set()<\/em> from last week&#8217;s Lesson:<\/p>\n<pre class=\"screen\">\r\nint bit_test(char bit, char byte)\r\n{\r\n    bit = 1 &lt;&lt; bit;\r\n    return(bit &amp; byte);\r\n}<\/pre>\n<p>The first statement sets the proper position for variable <code>bit<\/code>. The second statement uses the <code>&amp;<\/code> (and) mask to isolate that bit and return the value.<\/p>\n<p>The value returned by <em>bit_test()<\/em> is either true, meaning the bit was set, or false when the bit is zero.<\/p>\n<p>The final bit manipulation function is <em>bit_reset()<\/em>, which resets a bit&#8217;s value to zero. This function uses the same arguments as <em>bit_set()<\/em> and <em>bit_test()<\/em>, which provides consistency.<\/p>\n<p>To reset a bit, the <em>bit_reset()<\/em> function uses both the bitwise <code>^<\/code> (exclusive or) and <code>&amp;<\/code> (and) operators. These operators help to isolate the individual bit and set only that bit to 0.<\/p>\n<p>Here is the function:<\/p>\n<pre class=\"screen\">\r\nvoid bit_reset(char bit, char *byte)\r\n{\r\n    bit = 1 &lt;&lt; bit;\r\n    bit ^= 0xff;\r\n    *byte = *byte &amp; bit;\r\n}<\/pre>\n<p>The statement <code>bit ^= 0xff;<\/code> inverts the bits. The net effect is to mask off all bits save for the one to reset, as illustrated in Figure 3.<\/p>\n<div id=\"attachment_1853\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1853\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure3-xor.png\" alt=\"Figure 3. The ^ (xor) operator inverts the bit field, creating a mask.\" width=\"450\" height=\"368\" class=\"size-full wp-image-1853\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure3-xor.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure3-xor-300x245.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure3-xor-367x300.png 367w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1853\" class=\"wp-caption-text\">Figure 3. The <code>^<\/code> (xor) operator inverts the bit field, creating a mask.<\/p><\/div>\n<p>Once the mask is set, the <code>&amp;<\/code> (and) operator filters through every bit but the one to reset. The end result is that only one bit is reset.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1866\">next week&#8217;s Lesson<\/a>, I put all three functions to work in a demo program that manipulates bits in a <em>char<\/em> value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More bit manipulation fun! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1848\">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-1848","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\/1848","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=1848"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1848\/revisions"}],"predecessor-version":[{"id":1893,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1848\/revisions\/1893"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}