tag:blogger.com,1999:blog-14074542.post576722863708534511..comments2023-09-15T14:12:23.832+05:30Comments on Gebo - Madhur Tanwani's blog: Find if there is only one bit set in a numberAnonymoushttp://www.blogger.com/profile/05009478527695401190noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-14074542.post-49275650702549638662013-01-04T04:28:46.909+05:302013-01-04T04:28:46.909+05:30N & (~N +1)N & (~N +1)Ravihttps://www.blogger.com/profile/13263768662756042008noreply@blogger.comtag:blogger.com,1999:blog-14074542.post-33636124140873863712009-09-28T13:07:37.075+05:302009-09-28T13:07:37.075+05:30Take any random number which is a power of 2. Its ...Take any random number which is a power of 2. Its binary can be shown as (0*)1(0*). Its complement will be (1*)0(1*). If I add 1 to it, it will become (1*)1(o*)<br />i.e. XXXX01111 + 1 = XXXX10000<br /><br />So, finally if you XOR this number with the actual number, you will get the same number again.<br /><br />That is the check you should do<br />ie. x ^ (x` + 1) == x.<br />Note that it will not hold for x=0. You should special case itMadan Kumarhttps://www.blogger.com/profile/08995567009390429357noreply@blogger.comtag:blogger.com,1999:blog-14074542.post-24683754242013874032009-08-29T00:30:04.302+05:302009-08-29T00:30:04.302+05:30Test fails if there are no bits set
in your examp...Test fails if there are no bits set<br /><br />in your example<br />n = 0<br />n' = 0<br />k = 0<br />k == 0Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-14074542.post-21417038772304376092009-08-17T16:03:42.012+05:302009-08-17T16:03:42.012+05:30The question is same as finding whether a number i...The question is same as finding whether a number is a power of 2 put in a different formTom Praisonhttps://www.blogger.com/profile/04558906098186662974noreply@blogger.comtag:blogger.com,1999:blog-14074542.post-34594461593840446452009-08-17T00:41:54.743+05:302009-08-17T00:41:54.743+05:30Working code :
public class ExactlyOneBitInNumbe...Working code : <br /><br />public class ExactlyOneBitInNumber {<br /> public static void main(String[] args) {<br /> int i = 8;<br /> int j = -(i);<br /> System.out.println(" i = " + i);<br /> System.out.println(" j = " + j);<br /> <br /> int k = (i & j);<br /> System.out.println(" k = " + k);<br /><br /> System.out.println();<br /><br /> i = 10;<br /> j = -(i);<br /> System.out.println(" i = " + i);<br /> System.out.println(" j = " + j);<br /><br /> k = (i & j);<br /> System.out.println(" k = " + k);<br /> }<br />}<br /><br />O/P : <br /> i = 8<br /> j = -8<br /> k = 8<br /><br /> i = 10<br /> j = -10<br /> k = 2Anonymoushttps://www.blogger.com/profile/05009478527695401190noreply@blogger.comtag:blogger.com,1999:blog-14074542.post-2403414041004740272009-08-17T00:41:06.632+05:302009-08-17T00:41:06.632+05:30We can use the 2 complements method to find this.
...We can use the 2 complements method to find this.<br /><br /> - Let number be n<br /> - Take 2's complement of n. Let this be n'<br /> - BITWISE AND n and n'. Let this result be k<br /> - if k == n, then n has exactly one '1' bit<br /><br />e.g. Let number of '8'<br />n = 8 (....01000)<br />n' = ....11000<br />k = 8 (....01000)<br /><br /><br />e.g. Let number of '10'<br />n = 10 (....01010)<br />n' = ....10110<br />k = 2 (....00010)Anonymoushttps://www.blogger.com/profile/05009478527695401190noreply@blogger.com