{"id":193,"date":"2012-05-01T22:18:04","date_gmt":"2012-05-01T22:18:04","guid":{"rendered":"https:\/\/www.cores2.com\/blog\/?p=193"},"modified":"2012-05-01T22:20:09","modified_gmt":"2012-05-01T22:20:09","slug":"little-trick-for-bit-wise-optimization-integer-bound-optimization","status":"publish","type":"post","link":"https:\/\/www.cores2.com\/blog\/?p=193","title":{"rendered":"Little trick for bit-wise optimization integer bound optimization"},"content":{"rendered":"<p>If you need to bind an integer between 0 and powers of 2 &#8211; 1(i.e. 1, 3, 7, 15, 31, etc.), you can use the bit-wise and operator to mask out irrelevant bits rather than apply modulo. The idea is I\u00c2\u00a0believe\u00c2\u00a0that this is faster to do on a processor than to do the\u00c2\u00a0modulo\u00c2\u00a0operator \/ code. It might even be the case that modern C compilers optimize for this! I&#8217;m even more curious if the hardware (x86) is smart enough to do this.<\/p>\n<p>Put simply: essentially you take your integer, and apply bit-wise and logic to the number, so that all bits that represent a number higher than your limit are ignored, but yet you retain the relavent bits forming a binary number within your limits. You need to do this with powers of 2 &#8211; 1 because those numbers are all &#8220;filled&#8221; bit representations, so for example 3 is 11, 7 is 111, 15 is 1111.<\/p>\n<p>Examples:<\/p>\n<p>1. Simple \/ standard approach:<br \/>\nint Bounded = rand() % 256<\/p>\n<p>2. Bit-wise approach:<br \/>\nint Bounded = rand() &amp; 255;<\/p>\n<p>Same end result, but I believe (though without confirmation) that approach #2 is faster. Let me hear your thoughts!<\/p>\n<p><strong>Edit:<\/strong>\u00c2\u00a0As a friend just pointed out to me (he is a Comp. Eng. major, in my defense :P), I&#8217;m right that this optimization is commonly found in compilers and some processors. Wikipedia has a slightly better <a href=\"http:\/\/en.wikipedia.org\/wiki\/Modulo_operation#Performance%5Fissues\">explanation on this corner-case optimization<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you need to bind an integer between 0 and powers of 2 &#8211; 1(i.e. 1, 3, 7, 15, 31, etc.), you can use the bit-wise and operator to mask out irrelevant bits rather than apply modulo. The idea is &hellip; <a href=\"https:\/\/www.cores2.com\/blog\/?p=193\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-193","post","type-post","status-publish","format-standard","hentry","category-news_updates"],"_links":{"self":[{"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=193"}],"version-history":[{"count":0,"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cores2.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}