{"id":34864,"date":"2016-05-26T14:36:49","date_gmt":"2016-05-26T09:06:49","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=34864"},"modified":"2024-01-02T17:46:24","modified_gmt":"2024-01-02T12:16:24","slug":"java-bitwise-operator-using-efficiently-to-improve-performance","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/java-bitwise-operator-using-efficiently-to-improve-performance\/","title":{"rendered":"Java Bitwise Operator using efficiently to improve Performance"},"content":{"rendered":"<p>Many of us know the various operators available in Java. But do we really use them all efficiently. Here I am illustrating one simple example which can show the high performance gain with bitwise operatator than the standard solution.<\/p>\n<p>Lets calculate the mathematical expression x<sup>n\u00a0<\/sup>i.e. x&#8217;s power n.<\/p>\n<p>To solve this problem anyone can easily say its just one loop with n\u00a0times and adding a in that loop. Thats very simple and fast. Really? Lets analyze it, we have just used O(n) complexity to find the result which means 8<sup>20<\/sup> will take 20 loop iterations to calculate it.<\/p>\n<pre>x<sup>n<\/sup> = x * x ...... * x (n times)\r\n<\/pre>\n<p>Do we really need so much iterations to calculate it?<\/p>\n<p>Following mathematical formula can help us to reduce the O(n) complexity\u00a0to O(log n) complexity<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/exponentiation.png\" alt=\"\" width=\"304\" height=\"130\" \/><\/p>\n<p>Bitwise operator can be used to achieve this by shifting the bits of power. Following sample code is self explainatory.<\/p>\n<p>[java]<br \/>\npublic class PowerAofB {<br \/>\n\tpublic static void main(String[] args) {<br \/>\n\t\t\/\/ Basic way with O(n)<br \/>\n\t\tint a = 9;<br \/>\n\t\tint b = 12;<br \/>\n\t\tlong result = 1;<br \/>\n\t\tfor (int i = 1; i &lt;= b; i++) {<br \/>\n\t\t\t\tresult *= a;<br \/>\n\t\t}<br \/>\n\t\tSystem.out.println(result);<\/p>\n<p>\t\t\/\/ Efficient way with O(log n)<br \/>\n\t\tSystem.out.println(ipow(a,b));<br \/>\n\t}<\/p>\n<p>\tprivate static long ipow(int base, int exp)<br \/>\n\t{<br \/>\n\t    long result = 1;<\/p>\n<p>\t    while (exp != 0)<br \/>\n\t    {<br \/>\n\t    \tif ((exp &amp; 1) == 1){<br \/>\n\t            result *= base;<br \/>\n\t    \t}<br \/>\n\t        \/\/right shifting bytes with sign 1 position<br \/>\n\t        \/\/equivalent of division of 2<br \/>\n\t        exp &gt;&gt;= 1;<br \/>\n\t        base *= base;<br \/>\n\t    }<\/p>\n<p>\t    return result;<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Comparison between basic approach and new approach<\/p>\n<table style=\"border: 1px solid black\" cellspacing=\"0\">\n<tbody>\n<tr style=\"border: 1px solid black;vertical-align: middle\">\n<td style=\"border: 1px solid black;padding-right: 10px;padding-left: 10px\" align=\"left\" bgcolor=\"#9999FF\" height=\"35\">Approach<\/td>\n<td style=\"border: 1px solid black;padding-right: 10px;padding-left: 10px\" align=\"right\" bgcolor=\"#9999FF\">Example<\/td>\n<td style=\"border: 1px solid black;padding-right: 10px;padding-left: 10px\" align=\"right\" bgcolor=\"#9999FF\">iteration count<\/td>\n<td style=\"border: 1px solid black;padding-right: 10px;padding-left: 10px\" align=\"right\" bgcolor=\"#9999FF\">Execution time (nano seconds)<\/td>\n<\/tr>\n<tr style=\"border: 1px solid black;vertical-align: middle\">\n<td style=\"border: 1px solid black\" align=\"left\" bgcolor=\"#CCCCCC\" height=\"50\">Basic Way<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">12<small><sup>15<\/sup><\/small><\/td>\n<td style=\"border: 1px solid black\" align=\"right\">15<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">1140<\/td>\n<\/tr>\n<tr style=\"border: 1px solid black;vertical-align: middle\">\n<td style=\"border: 1px solid black\" align=\"left\" bgcolor=\"#CCCCCC\" height=\"50\">Using Bitwise Operator<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">12<small><sup>15<\/sup><\/small><\/td>\n<td style=\"border: 1px solid black\" align=\"right\">5<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">570<\/td>\n<\/tr>\n<tr style=\"border: 1px solid black;vertical-align: middle\">\n<td style=\"border: 1px solid black\" align=\"left\" bgcolor=\"#CCCCCC\" height=\"50\">Basic Way<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">4<sup><small>122<\/small><\/sup><\/td>\n<td style=\"border: 1px solid black\" align=\"right\">122<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">4561<\/td>\n<\/tr>\n<tr style=\"border: 1px solid black;vertical-align: middle\">\n<td style=\"border: 1px solid black\" align=\"left\" bgcolor=\"#CCCCCC\" height=\"50\">Using Bitwise Operator<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">4<sup><small>122<\/small><\/sup><\/td>\n<td style=\"border: 1px solid black\" align=\"right\">8<\/td>\n<td style=\"border: 1px solid black\" align=\"right\">1710<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><small style=\"color:blue\">Image\u00a0Reference:\u00a0http:\/\/www.programminglogic.com\/fast-exponentiation-algorithms\/ <\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many of us know the various operators available in Java. But do we really use them all efficiently. Here I am illustrating one simple example which can show the high performance gain with bitwise operatator than the standard solution. Lets calculate the mathematical expression xn\u00a0i.e. x&#8217;s power n. To solve this problem anyone can easily [&hellip;]<\/p>\n","protected":false},"author":922,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[446,1994,1],"tags":[3397,4844,3396,2683],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34864"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/922"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=34864"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34864\/revisions"}],"predecessor-version":[{"id":59857,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34864\/revisions\/59857"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=34864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=34864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=34864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}