@sunny that will work fine(xoring). In place of Xoring u can also do OR of two number and find the distance between fist set bit from left and first set bit from right,
Since bit operation is really fast operation so best algo this is of complexity O(1); Explanation How it works: In l only one bit will be set. In m multiple bit would be set with all bit before the bit set in l. NOW suppose u divde l in 2 parts means u have shifted set bit of l to the right. dividing again will shift set bit in l to the right. u have to divide till set bit in l reached the position where first bit of m is set. how many times u have shifted is count of dividng the rope its Simple. u can check with any example Thanks Santosh On Sat, Jul 2, 2011 at 2:41 PM, sunny agrawal <[email protected]>wrote: > for a number N > first set bit(From Left) is simply integer value of log(N) > last set bit can be calculated as > > N = N-(N&(N-1)); and then Log(N) > > int i = log(n); > n -= n&(n-1); > int j = log(n); > > i-j will be the answer. > > > > On Sat, Jul 2, 2011 at 2:34 PM, cegprakash <[email protected]> wrote: > >> oh fine.. got it now.. set bit is '1' right.. and is there any short >> ways to find the difference between first set and short set bit >> without dividing by 2 repeatedly? >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > > > -- > Sunny Aggrawal > B-Tech IV year,CSI > Indian Institute Of Technology,Roorkee > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
