Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:35:02+00:00 2026-06-09T06:35:02+00:00

I need to check a given byte or series of bytes for a particular

  • 0

I need to check a given byte or series of bytes for a particular sequence of bits as follows:

  • Can start with zero or more number of 0s.
  • Can start with zero or more number of 1s.
  • Must contain at least one 0 at the end.

In other words, if the value of bytes is not 0, then we are only interested in values that contain consecutive 1s followed by at least one 0 at the end.

I wrote the following code to do just that but wanted to make sure that it highly optimized. I feel that the multiple checks within the if branches could be optimized but am not sure how. Please advise.

// The parameter [number] will NEVER be negative.
public static bool ConformsToPattern (System.Numerics.BigInteger number)
{
    byte [] bytes = null;
    bool moreOnesPossible = true;

    if (number == 0) // 00000000
    {
        return (true); // All bits are zero.
    }
    else
    {
        bytes = number.ToByteArray();

        if ((bytes [bytes.Length - 1] & 1) == 1)
        {
            return (false);
        }
        else
        {
            for (byte b=0; b < bytes.Length; b++)
            {
                if (moreOnesPossible)
                {
                    if
                    (
                        (bytes [b] == 1) // 00000001
                        || (bytes [b] == 3) // 00000011
                        || (bytes [b] == 7) // 00000111
                        || (bytes [b] == 15) // 00001111
                        || (bytes [b] == 31) // 00011111
                        || (bytes [b] == 63) // 00111111
                        || (bytes [b] == 127) // 01111111
                        || (bytes [b] == 255) // 11111111
                    )
                    {
                        // So far so good. Continue to the next byte with
                        // a possibility of more consecutive 1s.
                    }
                    else if
                    (
                        (bytes [b] == 128) // 10000000
                        || (bytes [b] == 192) // 11000000
                        || (bytes [b] == 224) // 11100000
                        || (bytes [b] == 240) // 11110000
                        || (bytes [b] == 248) // 11111000
                        || (bytes [b] == 252) // 11111100
                        || (bytes [b] == 254) // 11111110
                    )
                    {
                        moreOnesPossible = false;
                    }
                    else
                    {
                        return (false);
                    }
                }
                else
                {
                    if (bytes [b] > 0)
                    {
                        return (false);
                    }
                }
            }
        }
    }

    return (true);
}

IMPORTANT: The argument [number] sent to the function will NEVER be negative so no need to check for the sign bit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:35:03+00:00Added an answer on June 9, 2026 at 6:35 am

    How about something like this? If you find a one, the only things after that can be 1s until a 0 is found. After that, only 0s. This looks like it’ll do the trick a little faster because it doesn’t do unnecessary or conditions.

    // The parameter [number] will NEVER be negative.
    public static bool ConformsToPattern (System.Numerics.BigInteger number)
    {
        byte [] bytes = null;
        bool moreOnesPossible = true;
        bool foundFirstOne = false;
    
        if (number == 0) // 00000000
        {
            return (true); // All bits are zero.
        }
        else
        {
            bytes = number.ToByteArray();
    
            if ((bytes [bytes.Length - 1] & 1) == 1)
            {
                return (false);
            }
            else
            {
                for (byte b=0; b < bytes.Length; b++)
                {
                    if (moreOnesPossible)
                    {
                        if(!foundFirstOne)
                        {
                            if
                            (
                                (bytes [b] == 1) // 00000001
                                || (bytes [b] == 3) // 00000011
                                || (bytes [b] == 7) // 00000111
                                || (bytes [b] == 15) // 00001111
                                || (bytes [b] == 31) // 00011111
                                || (bytes [b] == 63) // 00111111
                                || (bytes [b] == 127) // 01111111
                                || (bytes [b] == 255) // 11111111
                            )
                            {
                                foundFirstOne = true;
                                // So far so good. Continue to the next byte with
                                // a possibility of more consecutive 1s.
                            }
                            else if
                            (
                                (bytes [b] == 128) // 10000000
                                || (bytes [b] == 192) // 11000000
                                || (bytes [b] == 224) // 11100000
                                || (bytes [b] == 240) // 11110000
                                || (bytes [b] == 248) // 11111000
                                || (bytes [b] == 252) // 11111100
                                || (bytes [b] == 254) // 11111110
                            )
                            {
                                moreOnesPossible = false;
                            }
                            else
                            {
                                return (false);
                            }
                        }
                        else
                        {
                            if(bytes [b] != 255) // 11111111
                            {
                                if
                                (
                                    (bytes [b] == 128) // 10000000
                                    || (bytes [b] == 192) // 11000000
                                        || (bytes [b] == 224) // 11100000
                                    || (bytes [b] == 240) // 11110000
                                    || (bytes [b] == 248) // 11111000
                                    || (bytes [b] == 252) // 11111100
                                    || (bytes [b] == 254) // 11111110
                                )
                                {
                                    moreOnesPossible = false;
                                }
                            }                            
                        }
                    }
                    else
                    {
                        if (bytes [b] > 0)
                        {
                            return (false);
                        }
                    }
                }
            }
        }
    
        return (true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check if given date is after today 23:59:59, how can I
I need to check if a postcode is within a given start and end
Using JavaScript, I need to check if a given string contains a sequence of
I need check whether a given array will match another array. I can't figure
I need to check whether a given date is in the preceding month for
i need to check existing of Character (*,&,$) in Given String using python command
I need to check a variable vi_theIndex for its value. At the given moment
I need to check if entire given input matches given pattern. But wrapping a
I need to check if a given object implements an interface. In C# I
Need to check wether a given date is in between two given dates using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.