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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:16:18+00:00 2026-05-15T00:16:18+00:00

I have method for converting array of Booleans to integer. It looks like this

  • 0

I have method for converting array of Booleans to integer. It looks like this

class Program
    {
        public static int GivMeInt(bool[] outputs)
        {
            int data = 0;
            for (int i = 0; i < 8; i++)
            {
                data += ((outputs[i] == true) ? Convert.ToInt32(Math.Pow(2, i)) : 0);
            }
            return data;

        }

        static void Main(string[] args)
        {
            bool[] outputs = new bool[8];
            outputs[0] = false;
            outputs[1] = true;
            outputs[2] = false;
            outputs[3] = true;
            outputs[4] = false;
            outputs[5] = false;
            outputs[6] = false;
            outputs[7] = false;
            int data = GivMeInt(outputs);
            Console.WriteLine(data);
            Console.ReadKey();
        }
   }

Now I want to make opposite method returning array of Booleans values
As I am short with knowledge of .NET and C# until now I have only my mind hardcoding of switch statement or if conditions for every possible int value.

public static bool[] GiveMeBool(int data)
        {
            bool[] outputs = new bool[8];

            if (data == 0)
            {
                outputs[0] = false;
                outputs[1] = false;
                outputs[2] = false;
                outputs[3] = false;
                outputs[4] = false;
                outputs[5] = false;
                outputs[6] = false;
                outputs[7] = false;
            }
            //After thousand lines of coed
            if (data == 255)
            {
                outputs[0] = true;
                outputs[1] = true;
                outputs[2] = true;
                outputs[3] = true;
                outputs[4] = true;
                outputs[5] = true;
                outputs[6] = true;
                outputs[7] = true;
            }
            return outputs;
        }

I know that there must be easier way.

  • 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-15T00:16:19+00:00Added an answer on May 15, 2026 at 12:16 am

    You need to use bitwise operators: (Tested)

    public static bool[] GiveMeBool(int data) {
        bool[] outputs = new bool[8];
    
        for(int i = 0; i < 8; i++)
            outputs[i] = (data & (1 << i)) == (1 << i);
    
        return outputs;
    }
    

    You can also use bitwise operators to make your original much faster: (Untested)

    public static int GivMeInt(bool[] outputs) {
        int data = 0;
        for (int i = 0; i < 8; i++) 
            data += outputs[i] ? 1 << i : 0;
    
        return data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

normally JavaScript’s toString() method returns the array in a comma seperated value like this
I'd like to convert an array of integer values I have received over USB
I have a Hex Array which looks like : 31 31 00 00 00
I have this following method that connectss to db and returns a json array.
I have method that returns module path of given class name def findModulePath(path, className):
I have method in a class that I need to make sure is only
I have a method that converts an int to a base60 string (using 0-9,
I have a class cnList which stores a flexible number of items. This list
I have a little problem with converting a char string array to an unsigned
I have a C# method that returns very large number of objects. This is

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.