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

  • SEARCH
  • Home
  • 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 8331903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:33:43+00:00 2026-06-09T02:33:43+00:00

The following code checks to see if a given number follows a specific binary

  • 0

The following code checks to see if a given number follows a specific binary pattern.

I wrote this code without considering endian order and how the number is signed.

public static bool IsDiagonalToPowerOfTwo (this 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] == 255)
                    {
                        // Continue.
                    }
                    else if
                    (
                        ((bytes [b] & 128) == 128) // 10000000
                        || ((bytes [b] & 192) == 192) // 11000000
                        || ((bytes [b] & 224) == 224) // 11100000
                        || ((bytes [b] & 240) == 240) // 11110000
                        || ((bytes [b] & 248) == 248) // 11111000
                        || ((bytes [b] & 252) == 252) // 11111100
                        || ((bytes [b] & 254) == 254) // 11111110
                    )
                    {
                        moreOnesPossible = false;
                    }
                    else
                    {
                        return (false);
                    }
                }
                else
                {
                    if (bytes [b] > 0)
                    {
                        return (false);
                    }
                }
            }
        }
    }

    return (true);
}

How could I adjust this code to accommodate little endian order and sign? I have tried to follow MSDN but without luck.

  • 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-09T02:33:44+00:00Added an answer on June 9, 2026 at 2:33 am

    The test

    else if
            (
                ((bytes [b] & 128) == 128) // 10000000
                || ((bytes [b] & 192) == 192) // 11000000
                || ((bytes [b] & 224) == 224) // 11100000
                || ((bytes [b] & 240) == 240) // 11110000
                || ((bytes [b] & 248) == 248) // 11111000
                || ((bytes [b] & 252) == 252) // 11111100
                || ((bytes [b] & 254) == 254) // 11111110
            )
    

    can be reduced to else if ((bytes[b] & 128) == 128). Any of the later tests implies the first, so that already determines the outcome completely. I think what you really want here is

    else if (bytes[b] == 128
             || bytes[b] == 192
             || bytes[b] == 224
             || bytes[b] == 240
             || bytes[b] == 248
             || bytes[b] == 252
             || bytes[b] == 254
            )
    

    Apart from that, the representation is fixed, ToByteArray gives the same representation regardless of machine endianness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: QList<Vertex*> _vertices; //this gets filled //at some other point i
Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's
In the Android API example FragmentTabs.java I see the following constructor code inside the
I have this following code fragment which is accessed by different threads. try {
I have the following code that defines a getParts method to find a given
I have the following PHP code // Check if the upload is setted if
I am using following code to check whether a check box on my website
Please check following code tell me what the difference between Convert.ToInt32() and int() ,
Please check out my following code... public enum LogType { Debug, Info, Warn, Error,
Check the following code: ​<div onclick=alert('Hi, from outer div!');> <button onclick=alert('Hi, from button!');>Tha button</button>,

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.