there are two approaches we can take one is of anika i.e. shifting a no. right till it becomes 0 and count no. of shifts int main() { int x=10; int cnt=-1; while(x) { x>>=1; cnt++; } printf("%d",cnt); }
another is of aditya in which we didn't change the original no. instead take a new no. n=pow(2,15) for 16-bit and right shift it untill the first 1 occurs and displaying shift int main() { int x=10; int n=pow(2,15); int cnt=0; while(!(n & x)) { n>>=1; cnt++; } printf("%d",16-(cnt+1)); } On Thu, Jun 14, 2012 at 1:57 PM, aditya kumar <aditya.kumar130...@gmail.com>wrote: > for 16bit ..take a number n = 1000000000000000 > let x be the number mentionf in a question > pos = -1 > while (1){ > if(x & n){ > pos++ > exit} > else > n ยป=1 > } > > pos is the answer > if pos is greater than numbr of bits then x doesnt hv set bits > >> How to find the LEFT MOST set bit in an unsigned integer. >> Example: >> "00*1*0 0001 0100 0010" is a 16-bit unsigned integer. We have to find >> the left most set bit position (Starting with 0 from right side ) >> which is 13th position in our example. How to find this. Can any one pls >> give any suggestions. >> Thank You Very Much In Advance. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/algogeeks/-/HJnuFujMZ3wJ. >> 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. > -- Utsav Sharma, NIT Allahabad -- 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.