{"id":1522,"date":"2013-03-22T03:07:20","date_gmt":"2013-03-22T01:07:20","guid":{"rendered":"http:\/\/raftaman.net\/?p=1522"},"modified":"2021-05-15T11:46:38","modified_gmt":"2021-05-15T09:46:38","slug":"cc-binary-logic-operations","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=1522","title":{"rendered":"C\/C++ compound binary logic operations"},"content":{"rendered":"<p>I&#8217;ve always had trouble reading and understanding compound binary operations fluently. Whenever they appear in a piece of code, I have to decode them to actually understand what they are doing. This is mainly due to the fact that I infrequently work close to hardware were heavy bit manipulation is a daily occurrence. I won&#8217;t bother you with the truth tables of regular bitwise operators,  but here&#8217;s list of combined bit operations that come in quite handy:<\/p>\n<h2>Setting a bit<\/h2>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfoo |= 0x01;\r\n<\/pre>\n<p>The OR operation is used to set a particular bit that is specified in a BIT MASK. The bit mask is usually specified in hex. In this example it is <code>0x01<\/code> which indicates the first bit.<\/p>\n<h2>Clearing a bit<\/h2>\n<p>To clear bit 0 in <code>foo<\/code>, two different bit operations are required:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfoo &amp;= ~0x01;\r\n<\/pre>\n<p>This example uses the AND and the NOT operator. The NOT operator is used, because most programmers like to specify a mask, wherein the bit that should be changed, is set. You could of course just use a mask, wherein the bits that should be changed are unset, therefore eliminating the requirement for the NOT operator (note that this is highly uncommon though).<\/p>\n<h2>Flipping a bit<\/h2>\n<p>Another very useful tool is the XOR operator:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfoo ^= 0x01;\r\n<\/pre>\n<p>This toggles the first bit, independent of its current state and leaves all other bits unchanged.<\/p>\n<h2>Checking if a bit is set<\/h2>\n<p>Because a statement anything other than zero is considered <code>TRUE<\/code> in C\/C++ (and probably every other programming language, too), you can use <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nif(foo &amp; 0x01) {\r\n  (...)\r\n}\r\n<\/pre>\n<p>to check, whether the first bit is set or clear. <\/p>\n<h2>Dynamically building a bit mask<\/h2>\n<p>Because it is much easier to specify the bit <code>number<\/code> that should be changed rather than the actual bit <code>mask<\/code>, programmers usually build up the bit mask dynamically. This is done by left-shifting <code>0x01<\/code> n-times. For example, to build a bit mask that has bit number 7 set, you left-shift <code>0x01<\/code> seven times:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n(0x01 &lt;&lt; 7)\r\n<\/pre>\n<p>To build a bit mask that has the first bit (bit number 0) set, you&#8217;d shift <code>0x01<\/code> zero times:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n(0x01 &lt;&lt; 0)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve always had trouble reading and understanding compound binary operations fluently. Whenever they appear in a piece of code, I have to decode them to actually understand what they are doing. This is mainly due to the fact that I infrequently work close to hardware were heavy bit manipulation is a daily occurrence. I won&#8217;t bother you with the truth&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=1522\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[57],"class_list":["post-1522","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cc"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1522"}],"version-history":[{"count":10,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1522\/revisions"}],"predecessor-version":[{"id":2660,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1522\/revisions\/2660"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}