{"id":33,"date":"2005-12-06T05:35:42","date_gmt":"2005-12-06T10:35:42","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/processing\/33"},"modified":"2007-08-06T23:41:17","modified_gmt":"2007-08-07T04:41:17","slug":"bit-masking-in-processing","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/33\/","title":{"rendered":"Bit Masking in Processing"},"content":{"rendered":"<p>Sometimes you want to send a binary value into Processing (say, from a microcontroller) and check the individual bits of the value.  This example checks the bits of an 8-bit number individually, and reports whether each one is a 0 or a 1.<\/p>\n<p><!--more--><\/p>\n<pre>\n\/*\n  Checking a number's bits with a bitmask\n  by Tom Igoe\n  Checks the bits of a number one at a time using the shift (&lt;&lt;) \n  and bitwise AND (&#38;) functions.  \n  Updated 6 December 2005\n  \n*\/\n\/\/ get a random number between 0 and 255, convert to a byte:\nbyte someNumber = (byte)random(255);\n\/\/ loop over the number one bit at a time:\nfor (int i = 0; i &lt;= 7; i++) {\n    \/\/print the number as an 8-bit binary number:\n  println(\"someNumber = \" + binary(someNumber, 8));\n  \n  \/\/ print the bitmask as an 8-bit binary number:\n  byte bitmask = (byte)(1&lt;&lt;i);\n  println(\"bitmask    = \" + binary(bitmask, 8));\n  \n  \/\/ if the number has the bit in the bitmask set to 1, then say so:\n  if ((someNumber &#38; bitmask) == bitmask) {\n    println(\"bit \" + i + \" = 1\");\n  } \n  else {\n    println(\"bit \" + i + \" = 0\"); \n  }\n  \n  \/\/ print a couple of linefeeds for clean printing:\n  println(\"\\n\\n\");\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to send a binary value into Processing (say, from a microcontroller) and check the individual bits of the value.  This example checks the bits of an 8-bit number individually, and reports whether each one is a 0 or a 1.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}