{"id":538,"date":"2006-05-12T20:57:51","date_gmt":"2006-05-12T18:57:51","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2006\/05\/12\/538\/"},"modified":"2007-05-08T15:51:06","modified_gmt":"2007-05-08T13:51:06","slug":"php-bitwise-operators-example-of-use","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2006\/05\/php-bitwise-operators-example-of-use\/","title":{"rendered":"PHP Bitwise Operators Example of Use"},"content":{"rendered":"<p>In my neverending amateur journey through the wonders of coding, here is something I&#39;ve learnt in PHP today : how to conveniently store a set of flags into one value, with bitmask comparisons made easy thanks to <a href=\"http:\/\/www.php.net\/language.operators.bitwise\">PHP bitwise operators<\/a>. Sounds greek to you ? Read on, it&#39;s this kind of things that are piece of cake to use while they still make you look brilliant when you explain it to your wife.<br \/>\n<!--more--><\/p>\n<h2>Example of use<\/h2>\n<p>Say you&#39;re making a WordPress theme and want to give users control over whether a particular block should be in left sidebar or right sidebar (if displayed at all). To do so you&#39;ve set up this kind of form :<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/images\/display_preferences.gif\" alt=\"Sort of display preferences\" class=\"img\" \/><\/p>\n<h2>First : DEFINE<\/h2>\n<p>First, define a set of constants<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\ndefine(&#039;BLOCK_ARCHIVES&#039;\t, 1);\r\ndefine(&#039;BLOCK_CALENDAR&#039;\t, BLOCK_ARCHIVES &lt;&lt; 1);\r\ndefine(&#039;BLOCK_LASTCOM&#039;\t, BLOCK_ARCHIVES &lt;&lt; 2);\r\ndefine(&#039;BLOCK_QUOTE&#039;\t, BLOCK_ARCHIVES &lt;&lt; 3);\r\ndefine(&#039;BLOCK_CCNUMBER&#039;\t, BLOCK_ARCHIVES &lt;&lt; 4);\r\ndefine(&#039;BLOCK_BABE&#039;\t, BLOCK_ARCHIVES &lt;&lt; 5);\r\ndefine(&#039;BLOCK_ADMIN&#039;\t, BLOCK_ARCHIVES &lt;&lt; 6);\r\n?&gt;<\/code><\/pre><\/div>\n<p>All these constants now have the following values : 1, 2, 4, 8, 16, 32, 64 (powers of 2).<\/p>\n<h2>Now use<\/h2>\n<p>Once the user submits the form in the above screenshot, you would create and store two values, something like :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n$display_left = BLOCK_ARCHIVES | BLOCK_QUOTE | BLOCK_ADMIN;\r\n$display_right = BLOCK_CALENDAR | BLOCK_LASTCOM | BLOCK_BABE;\r\n?&gt;<\/code><\/pre><\/div>\n<p>The beauty of it is that, no matter how many options you have, you just need to store 2 integers to remember them all.<\/p>\n<p>Testing whether a block should display left, right or nowhere is now easy :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-3\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\nif (constant($block) === ($display_left &amp; constant($block))) {\r\n       echo &quot;$block goes left&quot;;\r\n} elseif (constant($block) === ($display_right &amp; constant($block))) {\r\n       echo &quot;$block goes right&quot;;\r\n} else {\r\n       echo &quot;$block goes \/dev\/null&quot;;\r\n}\r\n?&gt;<\/code><\/pre><\/div>\n<p>Testing the above code with <strong>$block = &#39;BLOCK_ARCHIVES&#39;<\/strong> would for example echo <strong>BLOCK_ARCHIVES goes left<\/strong>. Easy, isn&#39;t it ?<\/p>\n<h2>Other ideas of use<\/h2>\n<p>Really, this trick is a useful one. You could imagine storing a lot of things this way :<\/p>\n<ul>\n<li>display options for blocks in sidebars as explained above<\/li>\n<li>mail options for a forum in which you want the user to be able to specify if particular notices and messages are delivered via regular mail, internal messaging a la phpBB or just ignored ($send_email = ALERT | CHANGEPWD; $send_internal = USER_MSG | THREADWATCH_REPLY;)<\/li>\n<li>display advanced options or not depending on user profile ($access_full = ADMIN | MODERATOR | BIGBOSS; $access_restrict = GUEST | USER;)<\/li>\n<li>etc&#8230;<\/li>\n<\/ul>\n<h2>Notes<\/h2>\n<p>I believe the number of options you can specify is therefore depending on the plaftorm you&#39;re on (32 bits or 64 bits), which makes anyway a reasonable variety of user defined settings.<\/p>\n<p>Depending on the number of options you&#39;re having, you will be storing in a database small or great integers (up to 2^8-1 = 255 if you have 8 options, up to 16777216 if you have 24 options, etc&#8230;). You can therefore fine-tune your database scheme down to the smallest integer type needed for the option value field, i.e for MySQL TINYINT, SMALLINT, INT, etc&#8230;<\/p>\n<p>I have to thank <a href=\"http:\/\/www.rosanbo.com\/\">Polo<\/a>, my new PHP mentor, for sharing another bit of his knowledge with me :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my neverending amateur journey through the wonders of coding, here is something I&#39;ve learnt in PHP today : how to conveniently store a set of flags into one value, with bitmask comparisons made easy thanks to PHP bitwise operators. Sounds greek to you ? Read on, it&#39;s this kind of things that are piece of cake to use while\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,10],"class_list":["post-538","post","type-post","status-publish","format-standard","hentry","tag-code","tag-php"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=538"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/538\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}