{"id":1888,"date":"2016-04-30T00:01:46","date_gmt":"2016-04-30T07:01:46","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1888"},"modified":"2016-05-07T12:33:23","modified_gmt":"2016-05-07T19:33:23","slug":"reading-a-wide-bit-field","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1888","title":{"rendered":"Reading a Wide Bit Field"},"content":{"rendered":"<p>The same programming kung fu used to read a single bit also applies to wide bit fields. The process involves masking and shifting: You must know the bit field&#8217;s width and position in an integer value. Once those values are obtained &mdash; no matter how wide the bit field &mdash; a single function handles the job.<br \/>\n<!--more--><br \/>\nThe function I crafted to read a wide bit field is called <em>bit_field_read()<\/em>. Here is its prototype:<\/p>\n<p><code>unsigned char bit_field_read(char bit, char width, char byte);<\/code><\/p>\n<p>The argument <code>bit<\/code> is the position in the integer value, here limited to <em>unsigned char<\/em> variables. Position 0 is the far right bit and the values range up to 7 for the far left bit.<\/p>\n<p><code>width<\/code> is the size of the chunk to read, which can range from 1 to read a single bit or 8 to read the entire byte.<\/p>\n<p>Finally, <code>byte<\/code> is the value containing the bit field.<\/p>\n<p>My function doesn&#8217;t do any error checking, so it&#8217;s possible to read a bit not within the byte or a chunk wider than the byte. If I were coding <em>bit_field_read()<\/em> for use &#8220;in the wild&#8221; I would definitely add such bounds checking.<\/p>\n<p>What the <em>bit_field_read()<\/em> function does is to shift bits in the byte right so that the field starts at bit 0. Then the field width is masked. The result is the value desired. Figure 1 demonstrates how a bit field 4-bits wide at position 2 would be processed by the <em>bit_field_read()<\/em> function.<\/p>\n<div id=\"attachment_1895\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1895\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-bit_field.png\" alt=\"Figure 1. Processing a field 4-bits wide.\" width=\"550\" height=\"552\" class=\"size-full wp-image-1895\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-bit_field.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-bit_field-150x150.png 150w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-bit_field-300x300.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-bit_field-299x300.png 299w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-1895\" class=\"wp-caption-text\">Figure 1. Processing a field 4-bits wide.<\/p><\/div>\n<p>The tough part of the function is to convert the <code>width<\/code> value into a proper bit mask. For example, the value 2 specifies a width of two bits, but in binary the value 2 is <code>10<\/code> and the mask should be <code>11<\/code>, which is decimal 3. Table 1 shows how the width values need to be converted.<\/p>\n<table width=\"80%\">\n<tr>\n<th>Field width<\/th>\n<th>Binary mask<\/th>\n<th>Decimal value (unsigned)<\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>0000-0001<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>0000-0011<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>0000-0111<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>0000-1111<\/td>\n<td>15<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>0001-1111<\/td>\n<td>31<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>0011-1111<\/td>\n<td>63<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>0111-1111<\/td>\n<td>127<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>1111-1111<\/td>\n<td>255<\/td>\n<\/tr>\n<\/table>\n<p>To solve the puzzle, you must get from a value like 3 to the value 7. The solution I devised uses binary manipulation to add a bit to a byte, shift the value left, then keep looping to add bits. Here is that code snippet:<\/p>\n<pre class=\"screen\">\r\n    mask = 0;\r\n    while(width--)\r\n    {\r\n        mask = mask &lt;&lt; 1;\r\n        mask |= 1;\r\n    }<\/pre>\n<p>The variable <code>width<\/code> holds the original width value, such as 3. <code>mask<\/code> holds the final value, such as 7. The mask starts as 0. Its bits are shifted right, which has no effect when the loop begins. Then the <code>mask |= 1<\/code> statement sets bit 0. If the value of <code>width<\/code> is larger, the loop continues. Bit 0 is shifted to bit 1, then bit 0 is set again. Figure 2 illustrates this process.<\/p>\n<div id=\"attachment_1896\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1896\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure2-mask_building.png\" alt=\"Figure 2. How decimal widths are converted into binary masks.\" width=\"550\" height=\"722\" class=\"size-full wp-image-1896\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure2-mask_building.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure2-mask_building-229x300.png 229w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-1896\" class=\"wp-caption-text\">Figure 2. How decimal widths are converted into binary masks.<\/p><\/div>\n<p>In the end, the <code>width<\/code> value is converted the proper <code>mask<\/code> value, just as shown in Table 1. The <code>mask<\/code> value is used in the <em>bit_field_read()<\/em> function to properly mask bits in the given integer value.<\/p>\n<p>Here is a demo program that does the same conversions as shown in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1874\">last week&#8217;s Lesson<\/a>, though the <em>bit_field_read()<\/em> function is used instead:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar bit_field_read(char bit, char width, char byte);\r\n\r\nint main()\r\n{\r\n    unsigned char board;\r\n\r\n    board = 23;\r\n\r\n    printf(\"The Queen is at row %d, column %d\\n\",\r\n            bit_field_read(3,3,board)+1,\r\n            bit_field_read(0,3,board)+1\r\n            );\r\n\r\n    return(0);\r\n}\r\n\r\nchar bit_field_read(char bit, char width, char byte)\r\n{\r\n    unsigned char mask;\r\n\r\n    mask = 0;\r\n    while(width--)\r\n    {\r\n        mask = mask &lt;&lt; 1;\r\n        mask |= 1;\r\n    }\r\n\r\n    byte = byte &gt;&gt; bit;\r\n    return( byte &amp; mask );\r\n}<\/pre>\n<p>The output from the code is the same as last week:<\/p>\n<pre><code>The Queen is at row 3, column 8<\/code><\/pre>\n<p>The <em>bit_field_read()<\/em> function works on any size field from 1 bit wide to the entire byte. So the function can also be used to read on-off values similar to the <em>bit_test()<\/em> function from an <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1848\">earlier Lesson<\/a>.<\/p>\n<p>A <em>bit_field_write()<\/em> function involves a bit more manipulation to put a bit field into an integer value. I&#8217;ll demonstrate that function in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1906\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You don&#8217;t need much trickery to read only part of a byte. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1888\">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-1888","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\/1888","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=1888"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1888\/revisions"}],"predecessor-version":[{"id":1927,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1888\/revisions\/1927"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}