{"id":27,"date":"2013-05-04T00:01:30","date_gmt":"2013-05-04T08:01:30","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=27"},"modified":"2013-05-18T09:47:17","modified_gmt":"2013-05-18T17:47:17","slug":"binary-bit-fun","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=27","title":{"rendered":"Binary Bit Fun: |"},"content":{"rendered":"<p>May the 4th be with you! It&#8217;s Jedi day, or Star Wars day, or whatever. Here&#8217;s a binary puzzle for you, one which demonstrates how the bitwise OR operator can be used to set bits in an integer value.<br \/>\n<!--more--><\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *binbin(int n);\r\n\r\nint main()\r\n{\r\n    int input,result,bit,x;\r\n\r\n    bit = 1;\r\n    printf(\"Type a number between 0 and 255: \");\r\n    scanf(\"%d\",&amp;input);\r\n    printf(\"%s\\n========\\n\",binbin(input));\r\n    for(x=0;x<8;x++)\r\n    {\r\n        result = bit | input;\r\n        printf(\"%s\\n\",binbin(result));\r\n        bit*=2;\r\n    }\r\n    return(0);\r\n}\r\n\r\nchar *binbin(int n)\r\n{\r\n    static char bin[9];\r\n    int x;\r\n\r\n    for(x=0;x&lt;8;x++)\r\n    {\r\n        bin[x] = n &#038; 0x80 ? '1' : '0';\r\n        n &lt;&lt;= 1;\r\n    }\r\n    bin[x] = '\\0';\r\n    return(bin);\r\n}\r\n<\/pre>\n<p>The code asks for input, a teensy int value (8 bits). That value is then manipulated using the logical OR for each bit position, 0 through 7.<\/p>\n<p>Here's how the output looks when zero is input:<\/p>\n<p class=\"menu_indent\"><code>Type a number between 0 and 255: 0<br \/>\n00000000<br \/>\n========<br \/>\n00000001<br \/>\n00000010<br \/>\n00000100<br \/>\n00001000<br \/>\n00010000<br \/>\n00100000<br \/>\n01000000<br \/>\n10000000<\/code><\/p>\n<p>You can see the single bit cascading across each value. That bit is set sequentially no matter which value you input. The bit is always set by the bitwise |, which is why I call that operator the set-bit operator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a binary puzzle for you, one that demonstrates how the bitwise OR operator can be used to set bits in an integer value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=27\">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-27","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\/27","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=27"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}