{"id":116,"date":"2011-06-24T18:02:55","date_gmt":"2011-06-24T18:02:55","guid":{"rendered":"http:\/\/62.253.218.153\/~rpmsoft\/45rpmdump\/?p=116"},"modified":"2011-06-25T19:27:33","modified_gmt":"2011-06-25T19:27:33","slug":"snippet-of-code-3-dumping-bits","status":"publish","type":"post","link":"https:\/\/www.45rpmsoftware.com\/blog\/?p=116","title":{"rendered":"Snippet of Code #3 &#8211; Dumping Bits"},"content":{"rendered":"<p>Sometimes it&#8217;s useful to save a little memory by storing data as bits rather than bytes. \u00a0After all, if you&#8217;re storing a boolean then using a whole byte is rather wasteful. \u00a0In any event, sometime you might want to inspect individual bits when working on your program. \u00a0If you&#8217;ve ever needed to do this then this code will help.<!--more--><\/p>\n<pre>void dumpbits(char byte)\r\n{\r\n\tchar finalhash[13]={'\\0'};\r\n\tchar hexval[16] = {'0', '1', '2', '3', '4', '5', '6',\r\n                '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};\r\n\tunsigned char mask_table[] = { 0x01, 0x02, 0x04,\r\n                0x08, 0x10, 0x20, 0x40, 0x80 };\r\n\r\n\tfinalhash[0] = hexval[((byte &gt;&gt; 4) &amp; 0xF)];\r\n\tfinalhash[1] = hexval[byte &amp; 0x0F];\r\n\tfinalhash[2] = ' ';\r\n\tfor (int iterator=0;iterator&lt;8;iterator++)\r\n\t{\r\n\t\tif (( byte &amp; mask_table[iterator] ) != 0x00)\r\n\t\t\tfinalhash[3+iterator]='1';\r\n\t\telse\r\n\t\t\tfinalhash[3+iterator]='0';\r\n\t}\r\n\tprintf(\"%s \",finalhash);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it&#8217;s useful to save a little memory by storing data as bits rather than bytes. \u00a0After all, if you&#8217;re storing a boolean then using a whole byte is rather wasteful. \u00a0In any event, sometime you might want to inspect individual bits when working on your program. \u00a0If you&#8217;ve ever needed to do this then &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.45rpmsoftware.com\/blog\/?p=116\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Snippet of Code #3 &#8211; Dumping Bits&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/116"}],"collection":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":0,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}