{"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 id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'BLOCK_ARCHIVES'<\/span> <span style=\"color: #339933\">,<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'BLOCK_CALENDAR'<\/span> <span style=\"color: #339933\">,<\/span> BLOCK_ARCHIVES <span style=\"color: #339933\">&lt;&lt;<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'BLOCK_LASTCOM'<\/span>&nbsp; <span style=\"color: #339933\">,<\/span> BLOCK_ARCHIVES <span style=\"color: #339933\">&lt;&lt;<\/span> <span style=\"color: #cc66cc\">2<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'BLOCK_QUOTE'<\/span>&nbsp; &nbsp; <span style=\"color: #339933\">,<\/span> BLOCK_ARCHIVES <span style=\"color: #339933\">&lt;&lt;<\/span> <span style=\"color: #cc66cc\">3<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'BLOCK_CCNUMBER'<\/span> <span style=\"color: #339933\">,<\/span> BLOCK_ARCHIVES <span style=\"color: #339933\">&lt;&lt;<\/span> <span style=\"color: #cc66cc\">4<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'BLOCK_BABE'<\/span> <span style=\"color: #339933\">,<\/span> BLOCK_ARCHIVES <span style=\"color: #339933\">&lt;&lt;<\/span> <span style=\"color: #cc66cc\">5<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'BLOCK_ADMIN'<\/span>&nbsp; &nbsp; <span style=\"color: #339933\">,<\/span> BLOCK_ARCHIVES <span style=\"color: #339933\">&lt;&lt;<\/span> <span style=\"color: #cc66cc\">6<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$display_left<\/span> <span style=\"color: #339933\">=<\/span> BLOCK_ARCHIVES <span style=\"color: #339933\">|<\/span> BLOCK_QUOTE <span style=\"color: #339933\">|<\/span> BLOCK_ADMIN<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$display_right<\/span> <span style=\"color: #339933\">=<\/span> BLOCK_CALENDAR <span style=\"color: #339933\">|<\/span> BLOCK_LASTCOM <span style=\"color: #339933\">|<\/span> BLOCK_BABE<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">constant<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$block<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">===<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$display_left<\/span> <span style=\"color: #339933\">&amp;<\/span> <span style=\"color: #990000\">constant<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$block<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;<span style=\"color: #006699;font-weight: bold\">$block<\/span> goes left&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span> <span style=\"color: #b1b100\">elseif<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">constant<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$block<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">===<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$display_right<\/span> <span style=\"color: #339933\">&amp;<\/span> <span style=\"color: #990000\">constant<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$block<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;<span style=\"color: #006699;font-weight: bold\">$block<\/span> goes right&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span> <span style=\"color: #b1b100\">else<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;<span style=\"color: #006699;font-weight: bold\">$block<\/span> goes \/dev\/null&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 [&hellip;]<\/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}]}}