{"id":398,"date":"2010-10-11T16:11:41","date_gmt":"2010-10-11T23:11:41","guid":{"rendered":"http:\/\/www.rodgreen.com\/?p=398"},"modified":"2010-10-11T16:11:41","modified_gmt":"2010-10-11T23:11:41","slug":"next-power-of-2","status":"publish","type":"post","link":"https:\/\/www.rodgreen.com\/?p=398","title":{"rendered":"Next power of 2.."},"content":{"rendered":"<p>I came across this little snippet of code* to find the next power of 2 and found it quite helpful &#8211; from what I understand it&#39;s also very efficient compared to other methods.<\/p>\n<p>&nbsp;<\/p>\n<pre>public int NextPowerOfTwo(int inInt)\n{\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>inInt--;\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>inInt = ( inInt &gt;&gt; 1 ) | inInt;\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>inInt = ( inInt &gt;&gt; 2 ) | inInt;\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>inInt = ( inInt &gt;&gt; 4 ) | inInt;\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>inInt = ( inInt &gt;&gt; 8 ) | inInt;\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>inInt = ( inInt &gt;&gt; 16 ) | inInt;\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>inInt++; \/\/ Val is now the next highest power of 2.\n<span class=\"Apple-tab-span\" style=\"white-space:pre\"> <\/span>return inInt;\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Link to original article regarding technique and actually a more detailed explanation of the method &#8211; <a href=\"http:\/\/acius2.blogspot.com\/2007\/11\/calculating-next-power-of-2.html\">Acius&#39; Snippets<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I came across this little snippet of code* to find the next power of 2 and found it quite helpful &#8211; from what I understand it&#39;s also very efficient compared to other methods. &nbsp; public int NextPowerOfTwo(int inInt) { inInt&#8211;; inInt = ( inInt &gt;&gt; 1 ) | inInt; inInt = ( inInt &gt;&gt; 2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,4,6,11,15,16],"tags":[27,51,53,58,59],"class_list":["post-398","post","type-post","status-publish","format-standard","hentry","category-code-tips","category-development","category-games","category-technical-art","category-tutorials","category-unity","tag-coding","tag-development","tag-games","tag-tutorials","tag-unity"],"_links":{"self":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/posts\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":0,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}