Re: [algogeeks] Re: validate bit pattern : MS question
@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 1:23:28 AM UTC-5, ashgoel wrote: 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: @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 zeros. !(n = n+1) is true if there are only trailing ones, i.e., the original number was zeros followed by ones. n |= n-1 replaces trailing zeros with ones. Thus, if the original number is ones followed by zeros followed by ones, the zeros have been changed to ones. (n (n+1)) replaces the trailing ones with zeros. If the number is now zero, the number is valid, otherwise the number is invalid. Dave Superb, Dave!! On Tuesday, April 3, 2012 7:00:36 PM UTC-5, ashgoel 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 view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/zBATGHVXUTAJ. 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. -- 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/-/CALalzbiOI8J. 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.
Re: [algogeeks] Re: validate bit pattern : MS question
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: @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 zeros. !(n = n+1) is true if there are only trailing ones, i.e., the original number was zeros followed by ones. n |= n-1 replaces trailing zeros with ones. Thus, if the original number is ones followed by zeros followed by ones, the zeros have been changed to ones. (n (n+1)) replaces the trailing ones with zeros. If the number is now zero, the number is valid, otherwise the number is invalid. Dave Superb, Dave!! On Tuesday, April 3, 2012 7:00:36 PM UTC-5, ashgoel 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 view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/zBATGHVXUTAJ. 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. -- 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.
Re: [algogeeks] Re: validate bit pattern : MS question
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) on step 3 we return~(111000) = 1 for 1010011 after step 1 it becomes 10100111010100=101 after step2 it becomes 101|100=101 on step 3 we return~(101110) = 0 what are we trying to do with step 3 here Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sat, Apr 7, 2012 at 11:24 AM, Dave dave_and_da...@juno.com wrote: bool OnesZerosOnes(unsigned int n) { if( !(n 1) || !(n = n+1) ) return 0; n |= n-1; return !(n (n+1)); } -- 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.
Re: [algogeeks] Re: validate bit pattern : MS question
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 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) on step 3 we return~(111000) = 1 for 1010011 after step 1 it becomes 10100111010100=101 after step2 it becomes 101|100=101 on step 3 we return~(101110) = 0 what are we trying to do with step 3 here Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sat, Apr 7, 2012 at 11:24 AM, Dave dave_and_da...@juno.com wrote: bool OnesZerosOnes(unsigned int n) { if( !(n 1) || !(n = n+1) ) return 0; n |= n-1; return !(n (n+1)); } -- 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.
Re: [algogeeks] Re: validate bit pattern : MS question
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 ashg...@gmail.com wrote: 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) on step 3 we return~(111000) = 1 for 1010011 after step 1 it becomes 10100111010100=101 after step2 it becomes 101|100=101 on step 3 we return~(101110) = 0 what are we trying to do with step 3 here Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sat, Apr 7, 2012 at 11:24 AM, Dave dave_and_da...@juno.com wrote: bool OnesZerosOnes(unsigned int n) { if( !(n 1) || !(n = n+1) ) return 0; n |= n-1; return !(n (n+1)); } -- 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. -- *Piyush Khandelwal*** Mobile No: 91-8447229204 91-9808479765 -- 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.
Re: [algogeeks] Re: validate bit pattern : MS question
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 the number has trailing ones, n = n+1 replaces the trailing ones with zeros. !(n = n+1) is true if there are only trailing ones, i.e., the original number was zeros followed by ones. n |= n-1 replaces trailing zeros with ones. Thus, if the original number is ones followed by zeros followed by ones, the zeros have been changed to ones. (n (n+1)) replaces the trailing ones with zeros. If the number is now zero, the number is valid, otherwise the number is invalid. Dave Superb, Dave!! On Tuesday, April 3, 2012 7:00:36 PM UTC-5, ashgoel 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 view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/zBATGHVXUTAJ. 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.
[algogeeks] Re: validate bit pattern : MS question
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 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 view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/DFNF8rFO62UJ. 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.
[algogeeks] Re: validate bit pattern : MS question
@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 zeros. !(n = n+1) is true if there are only trailing ones, i.e., the original number was zeros followed by ones. n |= n-1 replaces trailing zeros with ones. Thus, if the original number is ones followed by zeros followed by ones, the zeros have been changed to ones. (n (n+1)) replaces the trailing ones with zeros. If the number is now zero, the number is valid, otherwise the number is invalid. Dave On Tuesday, April 3, 2012 7:00:36 PM UTC-5, ashgoel 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 view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/zBATGHVXUTAJ. 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.