@Atul007: Oops. Right. insert if( !(n & 1) ) return 0; before the first while statement. Dave
On Saturday, April 7, 2012 1:41:53 PM UTC-5, atul007 wrote: > e@Dave : just checked....it will fails for input .... 11110000 > small modification will make it work. > On 7 Apr 2012 21:13, "Dave" <dave_and_da...@juno.com> wrote: > >> @Atul007: A shorter and logically simpler version of this: >> >> bool IsOnesZerosOnes(unsigned int n) >> { >> while( n & 1 ) n >>= 1; >> if( !n ) return 0; >> while( !(n & 1 ) ) n >>= 1; >> while( n & 1 ) n >>= 1; >> return !n; >> } >> >> The first while loop strips off the trailing ones. >> If the result is nonzero, the second while strips off the zeros, and the >> third while strips off the next set of ones. >> If the result is zero, the n matches the pattern; otherwise, n fails to >> match it. >> >> Dave >> >> On Tuesday, April 3, 2012 11:57:16 PM UTC-5, atul007 wrote: >> >>> This can be done simply by checking and unchecking flag , below code >>> will work :- >>> >>> void checkPattern(int n) >>> { >>> int cnt=0,flag=1; >>> if(n&1) >>> { >>> while(n) >>> { >>> if(n&1 && flag==1) >>> { >>> cnt++; >>> flag=0; >>> } >>> else if(!(n&1) && flag==0) >>> { >>> cnt++; >>> flag=1; >>> } >>> if(cnt > 3) >>> { >>> break; >>> } >>> n=n>>1; >>> } >>> } >>> if(cnt==3) >>> printf("\npattern found\n"); >>> else >>> printf("\nPattern not found\n"); >>> } >>> >>> >>> >>> >>> On Wed, Apr 4, 2012 at 5:30 AM, Ashish Goel <ashg...@gmail.com> wrote: >>> >>>> verify that the bits of a number are in format 1s followed by 0s >>>> followed by 1s like 1110001 is valid but 100100100 is not >>>> >>>> Best Regards >>>> Ashish Goel >>>> "Think positive and find fuel in failure" >>>> +919985813081 >>>> +919966006652 >>>> >>>> -- >>>> 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+unsubscribe@** >>>> googlegroups.com <algogeeks%2bunsubscr...@googlegroups.com>. >>>> For more options, visit this group at http://groups.google.com/** >>>> group/algogeeks?hl=en <http://groups.google.com/group/algogeeks?hl=en>. >>>> >>> >>> -- >> 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/-/eJ4jTQHMOaYJ. >> 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 view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/Q0iGS5JdsTUJ. 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.