@sunny the no of set bits in m will tell what all length(4,2 in above case) are need to be merged. e.g if if m ==6 then m = 0110 since bit set position are 2 and 1. so length of rope need to combine is 2^2=4 and 2^1 = 2;i.e 4 and 2
Thnaks Santosh On Sat, Jul 2, 2011 at 2:58 PM, santosh mahto <santoshbit2...@gmail.com>wrote: > @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 > <sunny816.i...@gmail.com>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 <cegprak...@gmail.com> 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 algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> 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 algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> 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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.