int BitSwap(int A,int B) { int num=A^B; int count=0; while(num!=0) { num=num&(num-1); count++; } return count; }
this will run in O(m) where m is no. of ON bits (1's). is there any other optimal solution for finding no. of 1's in a number? On Sun, Jul 31, 2011 at 5:57 PM, prateek gupta <prateek00...@gmail.com>wrote: > C= A^B then count number of bits set in C. > > > On Sun, Jul 31, 2011 at 5:52 PM, Nikhil Gupta > <nikhilgupta2...@gmail.com>wrote: > >> Given two integers A & B. Determine how many bits required to convert A to >> B. Write a function int BitSwapReqd(int A, int B); >> >> -- >> Nikhil Gupta >> >> -- >> 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. >> > > > > -- > Prateek Gupta > 7th sem, Information Technology, > Netaji Subhas Institute Of Technology > Delhi. > > -- > 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. > -- Abhishek Gupta MCA NIT Calicut Kerela -- 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.