{"id":1819,"date":"2016-03-26T00:01:46","date_gmt":"2016-03-26T07:01:46","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1819"},"modified":"2016-04-02T08:51:10","modified_gmt":"2016-04-02T15:51:10","slug":"the-joy-of-bit-fields","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1819","title":{"rendered":"The Joy of Bit Fields"},"content":{"rendered":"<p>One of the C language&#8217;s strengths is that it can venture down into the depths of a computer&#8217;s inner workings. It loves to play in those dark, subterranean pools of raw data, bits, ports, and other primitive places that few high-level languages dare to tread.<br \/>\n<!--more--><br \/>\nSpecifically, the C language is great at manipulating bit fields.<\/p>\n<p>A <em>bit field<\/em> is where a byte, word, or larger chunk of data is manipulated one or more bits at a time. For example, an 8-bit byte (<em>char<\/em>) may represent 8 different binary values or toggles. This approach is more efficient than using 8 separate <em>int<\/em> variables to express on-off conditions.<\/p>\n<p>You find bit fields used in legacy programs, dating back to when memory or storage was tight. Still, even when you have gigabytes available, sometimes packing a dozen or so conditions into a single <em>int<\/em> makes sense. In the online world, bit fields are used frequently because it takes less time to transmit a 32-bit word than it does to transmit 32 words.<\/p>\n<p>An example of a bit field is the way colors are mapped in computer graphics. Three colors &mdash; red, green, and blue &mdash; are represented by three bits:<\/p>\n<p><code>100<\/code> = Red<br \/>\n<code>010<\/code> = Green<br \/>\n<code>001<\/code> = Blue<\/p>\n<p>As you run through the permutations of binary values <code>001<\/code> through <code>111<\/code>, you encounter the basic PC text graphics colors, as shown in the table below.<\/p>\n<table>\n<tr>\n<th width=\"33%\">Binary<\/th>\n<th width=\"34%\">Hex\/Dec<\/th>\n<th width=\"33%\">Color<\/th>\n<tr>\n<tr>\n<td>000<\/td>\n<td>0x00 \/ 0<\/td>\n<td>Black<\/td>\n<\/tr>\n<tr>\n<td>001<\/td>\n<td>0x01 \/ 1<\/td>\n<td>Blue<\/td>\n<\/tr>\n<tr>\n<td>010<\/td>\n<td>0x02 \/ 2<\/td>\n<td>Green<\/td>\n<\/tr>\n<tr>\n<td>011<\/td>\n<td>0x03 \/ 3<\/td>\n<td>Cyan<\/td>\n<\/tr>\n<tr>\n<td>100<\/td>\n<td>0x04 \/ 4<\/td>\n<td>Red<\/td>\n<\/tr>\n<tr>\n<td>101<\/td>\n<td>0x05 \/ 5<\/td>\n<td>Magenta<\/td>\n<\/tr>\n<tr>\n<td>110<\/td>\n<td>0x06 \/ 6<\/td>\n<td>Brown<\/td>\n<\/tr>\n<tr>\n<td>111<\/td>\n<td>0x07 \/ 7<\/td>\n<td>White<\/td>\n<\/tr>\n<\/table>\n<p>When the red\/green\/blue bits combine, they create the intermediate colors, such as blue and green (<code>011<\/code>) make cyan. Blue and red (<code>101<\/code>) create magenta. All bits off (<code>000<\/code>) is black. All bits on (<code>111<\/code>) is white.<\/p>\n<p>A fourth bit on the original PC (<code>1000<\/code>) set color intensity. When that bit is on, a second set of 8 colors is available, from gray (<code>1000<\/code> or &#8220;bright black&#8221;) through bright white (<code>1111<\/code>). The color &#8220;bright brown&#8221; (<code>1110<\/code>) is yellow.<\/p>\n<p>The text color bit field on the original PC was 8-bits wide in total. The first four bits set the background color, the second four set the text color.<\/p>\n<blockquote><p>As an early PC programmer, I eventually committed the bit field values to memory. I still remember that <code>0x04<\/code> is the text color red. In fact, the common acronym RGB helps me remember in which order the bits lie in the field: red, green, and blue.<\/p><\/blockquote>\n<p>PC text color is just one example of a bit field. In computerdom you&#8217;ll discover many bit fields. The C library uses bit fields in many functions and constants, but often macros are used to help manipulate them, so you don&#8217;t really get exposed directly. Regardless, bit fields allow for a lot of information to be contained in a tiny space.<\/p>\n<p>As an example, suppose you&#8217;ve written a computer game. In the game, you use a bit field to track the status of a starship. The bit field looks like this:<\/p>\n<p>Bit 0 &#8211; Transporter status<br \/>\nBit 1 &#8211; Warp Drive<br \/>\nBit 2 &#8211; Shields<br \/>\nBit 3 &#8211; Phasers<\/p>\n<p>Forgetting for a moment about Paramount Studios and their legions of lawyers defending copyrights, you need to code your game so that it can check the status of each of those bits, set a bit, and reset a bit. I&#8217;ll go over that process in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1836\">next week&#8217;s Lesson<\/a>. Before then, here are some bit field tidbits to ponder:<\/p>\n<ul>\n<li>Bits are referenced by their position within the byte, 0 through the width of the byte\/word\/long word.<\/li>\n<li>When looking at a binary representation of a bit field, bit 0 is on the far right.<\/li>\n<li>Generally speaking, a byte (<em>char<\/em>) is 8-bits wide.<\/li>\n<li>A word (<em>short int<\/em>) is 16-bits wide.<\/li>\n<li>A long word (<em>int<\/em>) is 32-bits wide.<\/li>\n<li>To set a bit is to assert its value to 1.<\/li>\n<li>To reset (or unset) a bit is to assert its value to 0.<\/li>\n<li>The value 1 is on or TRUE; the value 0 is off or FALSE.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Manipulate data at the lowest level. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1819\">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-1819","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\/1819","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=1819"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1819\/revisions"}],"predecessor-version":[{"id":1863,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1819\/revisions\/1863"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}