tag:blogger.com,1999:blog-8712770457197348465.post2303449542851015999..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: How to Check if Integer Number is Power of Two in Java - 3 examplesjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger18125tag:blogger.com,1999:blog-8712770457197348465.post-24398668453046681542017-01-13T02:29:55.762-08:002017-01-13T02:29:55.762-08:00@Miguel Duran, Exactly: So if you started with
i...@Miguel Duran, Exactly: So if you started with <br /><br />if(number < 1) return false; <br /><br />That would most likely be just as efficient an algorithm, but it would be more accurate (it would return false for 0) and it would be more orthogonal in the sense that it would return a consistent and correct answer over a wider range of input, rather than arbitrarily raising an exception.carbon14https://www.blogger.com/profile/00765686562946623679noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-52032417932982602102017-01-10T21:53:57.263-08:002017-01-10T21:53:57.263-08:00Infinity is not a value.Infinity is not a value.Anonymoushttps://www.blogger.com/profile/01626008114591762642noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4601629535413807022017-01-10T09:21:55.542-08:002017-01-10T09:21:55.542-08:00In the third example (using the bit-shift operator...In the third example (using the bit-shift operator) it checks to see if the number is less than 0 and raises an exception. Why!!<br />If the number is negative, then it's not a power of two (at least not a rational one), but that doesn't seem a good reason to declare number to be an illegal argument. Since the code already has a test why not test for number being less than 1 and carbon14https://www.blogger.com/profile/00765686562946623679noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71375118393285069062016-10-05T12:04:28.186-07:002016-10-05T12:04:28.186-07:0020 % 2 equals 0, but 20 is not a power of 2.20 % 2 equals 0, but 20 is not a power of 2.Anonymoushttps://www.blogger.com/profile/01626008114591762642noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69673637969393573622016-10-05T02:23:44.856-07:002016-10-05T02:23:44.856-07:00you know, you could've just used modulo on a n...you know, you could've just used modulo on a number by dividing it by 2 (num%2). if it equals to 0, then it's an even/power by 2 ;)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45889847564833398582016-06-10T09:11:11.840-07:002016-06-10T09:11:11.840-07:00Comments have a semantic error. Bit shift operato...Comments have a semantic error. Bit shift operators are <<, >>, and >>>. Solutions are using bit AND operator &.Johnny Hardcodehttps://www.blogger.com/profile/08134334275614058113noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20950000024664229942016-01-06T06:29:31.467-08:002016-01-06T06:29:31.467-08:00@TCool, you are correct, its not <= but just le...@TCool, you are correct, its not <= but just less than, corrected it nowjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-5357495200496548942016-01-05T19:54:41.131-08:002016-01-05T19:54:41.131-08:00For the first and third method, I think there is a...For the first and third method, I think there is a typo:<br />if(number<=0)<br />how can you display 0? And the result returns true?TCoolhttps://www.blogger.com/profile/06912113172867842656noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16429386970337421132015-06-28T02:22:27.761-07:002015-06-28T02:22:27.761-07:00Is this possible?
class two
{
public static void ...Is this possible?<br /><br />class two<br />{<br />public static void power(int num)<br />{<br />int dup= num ;<br />while (dup > 2)<br />{<br />if (dup==2)<br />System.out.print (num+"is a power of two");<br />else if (dup < 2)<br />System.out.print(num+"is not a power of two");<br />dup=dup/2;<br />}<br />}<br />By<br />RamAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-75056652787629939562014-11-18T19:50:36.351-08:002014-11-18T19:50:36.351-08:00return x == 0 ? false : x & (x - 1) == 0;return x == 0 ? false : x & (x - 1) == 0;Anonymoushttps://www.blogger.com/profile/01626008114591762642noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14735711110032942952013-07-30T15:54:47.043-07:002013-07-30T15:54:47.043-07:00Take logs and cast one side as int and check if tw...Take logs and cast one side as int and check if two sides are equal<br />public static boolean (int i)<br />return if((int)(Math.log(i+1)/Math.log(2)) - ((Math.log(i+1)/Math.log(2)))==0)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62170924369230611312013-06-26T20:23:32.885-07:002013-06-26T20:23:32.885-07:00WOW, Some clever tips checking integer is power of...WOW, Some clever tips checking integer is power of two, on comments :)Ravinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53736723517751372812013-06-15T10:30:22.299-07:002013-06-15T10:30:22.299-07:00You may just use Integer.bitcount(number) == 1You may just use Integer.bitcount(number) == 1Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59980633315080919652013-06-04T11:08:44.480-07:002013-06-04T11:08:44.480-07:00public static boolean isPowerOfTwo(int i){
if (i... public static boolean isPowerOfTwo(int i){<br /> if (i==0) return false;<br /> int d = Math.abs(i);<br /> return (d & (d-1)) == 0;<br /> }<br />Anonymoushttps://www.blogger.com/profile/09781254993618282351noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32894089855996716192013-06-03T23:27:49.947-07:002013-06-03T23:27:49.947-07:00I often read, heard that power of 2 numbers have s...I often read, heard that power of 2 numbers have special significance in computer world, Why? Why most HashMap, HashSet use sizes which is power of 2? WHYAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24010348078351443432013-06-02T09:40:12.179-07:002013-06-02T09:40:12.179-07:00Hi...i follow your blog while coding in my office....Hi...i follow your blog while coding in my office...I really find your site useful...recently i joined your site as I am also creating a blog for technical implementations. Its in a very early stage..<br /><br />Programming Language http://javacodeimpl.blogspot.in/Anonymoushttps://www.blogger.com/profile/07576885198163157451noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9286530097486994322013-06-01T03:37:19.153-07:002013-06-01T03:37:19.153-07:00what about this???
public class PowerOftwo
{
...what about this???<br /><br /><br />public class PowerOftwo <br />{<br /><br /> public void powerOftwo(int number)<br /> {<br /> int num = number;<br /> int d;<br /> boolean flag = true;<br /> while(num>1)<br /> {<br /> d = num % 2;<br /> if(d%2!=0)<br /> {<br /> flag = false;<br /> break;<br /> }<br /> num = num/2;<br saurabh chopranoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73980799285026810342013-05-30T13:54:41.324-07:002013-05-30T13:54:41.324-07:00Is the brute force case, O(logN) complexity since ...Is the brute force case, O(logN) complexity since you are multiplying by 2 each time? So log base 2 of N?Anonymousnoreply@blogger.com