{"id":427,"date":"2009-10-12T08:12:20","date_gmt":"2009-10-12T15:12:20","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2309"},"modified":"2009-10-12T08:12:20","modified_gmt":"2009-10-12T15:12:20","slug":"bitwise-or-and-variable-function-arguments","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=427","title":{"rendered":"Bitwise OR | and Variable Function Arguments (bitwise flags)"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nconst A:uint = 1;\r\nconst B:uint = 2;\r\nconst C:uint = 4;\r\nconst D:uint = 8;\r\nconst E:uint = 16;\r\n\r\nfunction orArgs(args:uint):void{\r\n\tif (args &amp; A){\r\n\t\ttrace(&quot;A&quot;);\r\n\t}\r\n\tif (args &amp; B){\r\n\t\ttrace(&quot;B&quot;);\r\n\t}\r\n\tif (args &amp; C){\r\n\t\ttrace(&quot;C&quot;);\r\n\t}\r\n\tif (args &amp; D){\r\n\t\ttrace(&quot;D&quot;);\r\n\t}\r\n\tif (args &amp; E){\r\n\t\ttrace(&quot;E&quot;);\r\n\t}\r\n}\r\n\r\n\/\/ test out the function: \r\norArgs(A | B);\r\ntrace(&quot;--&quot;);\r\norArgs(A | C | E);\r\ntrace(&quot;--&quot;);\r\norArgs(B | E | D);\r\ntrace(&quot;--&quot;);\r\norArgs(C | A);\r\n\r\n\/* outputs: \r\nA\r\nB\r\n--\r\nA\r\nC\r\nE\r\n--\r\nB\r\nD\r\nE\r\n--\r\nA\r\nC\r\n*\/\r\n<\/pre>\n<p>If you&#8217;ve every used Array.sort(Array.NUMERIC | Array.DESCENDING) you should have at least a vague idea about what this snippet is doing. It shows how you can pass a variable number of arguments to a function using | (bitwise OR) and &#038; (bitwise AND). I believe the correct term for these kind of arguments is &#8220;bitwise flags&#8221;. This snippet works by having a series of constant values&#8230; in this case A &#8211; E. Each constant is assigned an unsigned integer&#8230; now you may not see the significance of the values 1, 2, 4, 8 and 16 until you see them in binary&#8230; get ready this is a pretty cryptic description&#8230;<\/p>\n<p>A = 00001 = 1<br \/>\nB = 00010 = 2<br \/>\nC = 00100 = 4<br \/>\nD = 01000 = 8<br \/>\nE = 10000 = 16<\/p>\n<p>If we OR all these together we get: 11111&#8230; If we do:<\/p>\n<p> A | E<br \/>\n00001 | 10000 <\/p>\n<p>we end up with 10001&#8230;<\/p>\n<p>&#8230;we can then check which values are stored in the resulting unsigned integer by using AND:<\/p>\n<p>check for A&#8230; 10001 &#038; 00001 = 00001 = true<br \/>\ncheck for E&#8230; 10001 &#038; 10000 = 10000 = true<br \/>\ncheck for C&#8230; 10001 &#038; 00100 = 00000 = false<\/p>\n<p>That&#8217;s it&#8230; I just guessed at the way this was being done&#8230; if you have another way to do the same thing, feel free to post it in the comments&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>const A:uint = 1; const B:uint = 2; const C:uint = 4; const D:uint = 8; const E:uint = 16; function orArgs(args:uint):void{ if (args &amp; A){ trace(&quot;A&quot;); } if (args &amp; B){ trace(&quot;B&quot;); } if (args &amp; C){ trace(&quot;C&quot;); } if (args &amp; D){ trace(&quot;D&quot;); } if (args &amp; E){ trace(&quot;E&quot;); } } \/\/ test [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137,121,94,36],"tags":[3,14,38,4],"class_list":["post-427","post","type-post","status-publish","format-standard","hentry","category-binary","category-math","category-misc","category-operators","tag-actionscript","tag-as3","tag-binary","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=427"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/427\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}