{"id":1874,"date":"2016-04-23T00:01:34","date_gmt":"2016-04-23T07:01:34","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1874"},"modified":"2016-04-30T07:06:46","modified_gmt":"2016-04-30T14:06:46","slug":"wide-bit-fields","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1874","title":{"rendered":"Wide Bit Fields"},"content":{"rendered":"<p>An on-off bit field is pretty common; you&#8217;ll find them all over. Also common are wide bit fields, which can hold values greater than one or zero. These wide bit fields are often mixed with single-bit fields making for a wonderful stew of binary data all held within a single integer value.<br \/>\n<!--more--><br \/>\nAs an example, suppose that a chess game uses a bit field to keep track of a specific piece&#8217;s location. Two bit fields are required, one for the row and another for the column. The bit fields are organized as shown in Figure 1.<\/p>\n<div id=\"attachment_1879\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1879\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-row_colum_bit_fields.png\" alt=\"Figure 1. A byte containing two 3-bit fields.\" width=\"450\" height=\"223\" class=\"size-full wp-image-1879\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-row_colum_bit_fields.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure1-row_colum_bit_fields-300x149.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1879\" class=\"wp-caption-text\">Figure 1. A byte containing two 3-bit fields.<\/p><\/div>\n<p>In the figure, you see that three bits are used for both row and column values. Three bits express values from 0 through 7, which is enough to account for 8 rows: <code>000<\/code> through <code>111<\/code>. So if the Queen is at row 3, column 8, the binary equivalents would be:<\/p>\n<p>Row <code>010<\/code><br \/>\nColumn <code>111<\/code><\/p>\n<p>And the resulting byte would be:<\/p>\n<p><code>00010111<\/code><\/p>\n<p>The nifty part is that you don&#8217;t have to strain your head to assembly this binary information; the computer does the job for you. You just need to extract specific bits from the byte to obtain or set the Queen&#8217;s position. To code the solution, you need two of the bitwise operators used in previous lessons to extra values: bitwise <code>&amp;<\/code> (and) and the right-shift operator, <code>&gt;&gt;<\/code>.<\/p>\n<p>To obtain the row value, the rest of the byte is masked and then the values are shifted right three bits, as illustrated in Figure 2.<\/p>\n<div id=\"attachment_1880\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1880\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure2-row_mask_shift.png\" alt=\"Figure 2. Masking out the row value, then shifting that value right.\" width=\"450\" height=\"388\" class=\"size-full wp-image-1880\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure2-row_mask_shift.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure2-row_mask_shift-300x259.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure2-row_mask_shift-348x300.png 348w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1880\" class=\"wp-caption-text\">Figure 2. Masking out the row value, then shifting that value right.<\/p><\/div>\n<p>The equation <code>&amp; 0x38<\/code> masks out the three relevant bits. Next, those three bits are shifted to the right three places. The result is <code>0x02<\/code> (refer to Figure 2), which means that the Queen is at row 3 on the chessboard. (The first row is row zero.)<\/p>\n<p>To obtain the column, bits are masked as well, but the result need not be shifted, as shown in Figure 3.<\/p>\n<div id=\"attachment_1881\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1881\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure3-column_mask.png\" alt=\"Figure 3. Masking the column value.\" width=\"450\" height=\"287\" class=\"size-full wp-image-1881\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure3-column_mask.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/figure3-column_mask-300x191.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-1881\" class=\"wp-caption-text\">Figure 3. Masking the column value.<\/p><\/div>\n<p>After peeling off the rest of the byte with <code>&amp; 0x07<\/code>, the remaining value is the column. It just happens to be 7, which is coincidentally the same value as the <code>&amp;<\/code> (and) mask. And the 7 implies column 8 because, again, numbering starts at column zero.<\/p>\n<p>Here is code that extracts a chess piece&#8217;s location from an <em>unsigned char<\/em> value. The variable is <em>unsigned<\/em> so that negative results aren&#8217;t generated.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    unsigned char board,row,column;\r\n\r\n    board = 23;\r\n\r\n    row = board & 0x38;\r\n    row = row &gt;&gt; 3;\r\n    column = board &amp; 0x07;\r\n\r\n    printf(\"The Queen is at row %d, column %d\\n\",\r\n            row+1,column+1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is a sample run:<\/p>\n<pre><code>The Queen is at row 3, column 8<\/code><\/pre>\n<p>Because a <em>char<\/em> variable is (usually) 8-bits wide, I could have made the row and column bit fields 4-bits wide each. That would make manipulation easier, plus it would also make the code more readable to anyone who knows hexadecimal and its relationship to binary.<\/p>\n<p>For example, row 2 column 7 would be 0x27 hexadecimal. Also, the bitwise <code>&amp;<\/code> (and) masks would be <code>0xF0<\/code> and <code>0x0F<\/code>. Simple. But not every bit field is exactly 4-bits wide, so rarely will you be so lucky.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1888\">next week&#8217;s Lesson<\/a>, I cover a function that grabs a bit field of any length from any position within a byte.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A bit field need not be limited to a single bit. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1874\">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-1874","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\/1874","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=1874"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1874\/revisions"}],"predecessor-version":[{"id":1918,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1874\/revisions\/1918"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}