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 3391642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:49:33+00:00 2026-05-18T03:49:33+00:00

Seems elementary but I am not sure how to get each bit from a

  • 0

Seems elementary but I am not sure how to get each bit from a byte. Thanks for the help

  • 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-05-18T03:49:34+00:00Added an answer on May 18, 2026 at 3:49 am

    As RyuuGan already posted you should go with the BitArrary. You just put the data in it by calling the constructor with the wanted elements.

    byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 };
    BitArray bitArray = new BitArray( myBytes );
    

    Afterwards the instance has some interesting properties to easily access each bit. At first you can just call the index operator to get or set the state of each bit:

    bool bit = bitArray[4];
    bitArray[2] = true;
    

    Also you can enumerate over all bits by just using a foreach loop (or any LINQ-stuff you like)

    foreach (var bit in bitArray.Cast<bool>())
    {
        Console.Write(bit + " ");
    }
    

    To get back from the bits to some specific type (e.g. int) is a little bit trickier, but is quite easy using this extension methods:

    public static class Extensions
    {
        public static IList<TResult> GetBitsAs<TResult>(this BitArray bits) where TResult : struct
        {
            return GetBitsAs<TResult>(bits, 0);
        }
    
        /// <summary>
        /// Gets the bits from an BitArray as an IList combined to the given type.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="bits">An array of bit values, which are represented as Booleans.</param>
        /// <param name="index">The zero-based index in array at which copying begins.</param>
        /// <returns>An read-only IList containing all bits combined to the given type.</returns>
        public static IList<TResult> GetBitsAs<TResult>(this BitArray bits, int index) where TResult : struct
        {
            var instance = default(TResult);
            var type = instance.GetType();
            int sizeOfType = Marshal.SizeOf(type);
    
            int arraySize = (int)Math.Ceiling(((bits.Count - index) / 8.0) / sizeOfType);
            var array = new TResult[arraySize];
    
            bits.CopyTo(array, index);
    
            return array;
        }
    }
    

    With this in place you can just get out of it with this single line of code:

    IList<int> result = bitArray.GetBitsAs<int>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like an elementary question, but after a lot of searching around I
It seems elementary, but here is problem. Stylesheet like so: #Content h1, #Content h2,
This seems like an elementary question, but I can't find the answer anywhere: I
This seems like an easy thing, but I'm drawing a blank. Select * from
I'm sure this is elementary, but I am stumped. The example is grossly over-simplified
Seems like a simple enough question but I can't seem to find the answer.
Seems like as really simple thing to do, but I just can't track it
Seems that requirements on safety do not seem to like systems that use AI
Ok, so this may seem an elementary question. Maybe it is. But for the
Forgive my elementary question as I'm pretty new to cocos2d, but I'm having an

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.