Re: [algogeeks] Re: validate bit pattern : MS question

2012-05-19 Thread Dave
@Ashgoel: My solution worked when the pattern of ones followed by zeros followed by ones is right-justified in a larger integer, so that there are zeros to the left of the leftmost one. E.g., my algorithm will say that 00110011 in an 8-bit integer is valid. Dave On Friday, May 18, 2012

Re: [algogeeks] Re: validate bit pattern : MS question

2012-05-18 Thread Ashish Goel
i changed the last step to return (n == ~0); Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Fri, May 18, 2012 at 11:27 AM, Vishal Thanki vishaltha...@gmail.comwrote: On Sat, Apr 7, 2012 at 11:24 AM, Dave dave_and_da...@juno.com wrote:

Re: [algogeeks] Re: validate bit pattern : MS question

2012-05-17 Thread Ashish Goel
Hello Dave, Was trying this bool OnesZerosOnes(unsigned int n) { if( !(n 1) || !(n = n+1) ) return 0; /*step1*/ n |= n-1;/*step 2*/ return !(n (n+1)); /*step 3*/ } for 1110011 after step 1 it becomes 1110010100=111 after step2 it becomes 111|110=111(all ones)

Re: [algogeeks] Re: validate bit pattern : MS question

2012-05-17 Thread Ashish Goel
got it...super... Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Thu, May 17, 2012 at 8:21 PM, Ashish Goel ashg...@gmail.com wrote: Hello Dave, Was trying this bool OnesZerosOnes(unsigned int n) { if( !(n 1) || !(n = n+1) ) return

Re: [algogeeks] Re: validate bit pattern : MS question

2012-05-17 Thread Piyush Khandelwal
Hii Can anyone explain to me what this code is doing?? On 17 May 2012 20:36, Ashish Goel ashg...@gmail.com wrote: got it...super... Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Thu, May 17, 2012 at 8:21 PM, Ashish Goel

Re: [algogeeks] Re: validate bit pattern : MS question

2012-05-17 Thread Vishal Thanki
On Sat, Apr 7, 2012 at 11:24 AM, Dave dave_and_da...@juno.com wrote: @Ashgoel: Try this: bool OnesZerosOnes(unsigned int n) {     if( !(n 1) || !(n = n+1) ) return 0;     n |= n-1;     return !(n (n+1)); } Here is how it works: !(n 1) is true if the number has trailing zeros. If

[algogeeks] Re: validate bit pattern : MS question

2012-04-06 Thread Ilya Albrekht
Hello Ashish, I would do something like here - http://www.evaluzio.net/library/verify-1-0-bits-in-num.htm Basically you just count the number of 01 and 10 binary sequences using (n 3 == 1) or (n 3 == 2) Regards, Ilya On Tuesday, 3 April 2012 17:00:36 UTC-7, ashgoel wrote: verify that

[algogeeks] Re: validate bit pattern : MS question

2012-04-06 Thread Dave
@Ashgoel: Try this: bool OnesZerosOnes(unsigned int n) { if( !(n 1) || !(n = n+1) ) return 0; n |= n-1; return !(n (n+1)); } Here is how it works: !(n 1) is true if the number has trailing zeros. If the number has trailing ones, n = n+1 replaces the trailing ones with