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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:24:38+00:00 2026-05-27T10:24:38+00:00

I have 8 bool variables, and I want to merge them into a byte.

  • 0

I have 8 bool variables, and I want to “merge” them into a byte.

Is there an easy/preferred method to do this?

How about the other way around, decoding a byte into 8 separate boolean values?

I come in assuming it’s not an unreasonable question, but since I couldn’t find relevant documentation via Google, it’s probably another one of those “nonono all your intuition is wrong” cases.

  • 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-27T10:24:39+00:00Added an answer on May 27, 2026 at 10:24 am

    The hard way:

    unsigned char ToByte(bool b[8])
    {
        unsigned char c = 0;
        for (int i=0; i < 8; ++i)
            if (b[i])
                c |= 1 << i;
        return c;
    }
    

    And:

    void FromByte(unsigned char c, bool b[8])
    {
        for (int i=0; i < 8; ++i)
            b[i] = (c & (1<<i)) != 0;
    }
    

    Or the cool way:

    struct Bits
    {
        unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
    };
    union CBits
    {
        Bits bits;
        unsigned char byte;
    };
    

    Then you can assign to one member of the union and read from another. But note that the order of the bits in Bits is implementation defined.

    Note that reading one union member after writing another is well-defined in ISO C99, and as an extension in several major C++ implementations (including MSVC and GNU-compatible C++ compilers), but is Undefined Behaviour in ISO C++. memcpy or C++20 std::bit_cast are the safe ways to type-pun in portable C++.

    (Also, the bit-order of bitfields within a char is implementation defined, as is possible padding between bitfield members.)

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

Sidebar

Related Questions

Say I have this C# method: public bool GetVal() { return a1 == b1
I have a private variable in my class: BOOL isEnabled; and a method: -(void)
Let's say I have a method: bool myMethod(int a) { //return a bool }
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
I have a method like so private bool VerbMethod(string httpVerb, string methodName, string url,
I want to have such kind of work flow using explicit lock/condition variables (It's
I have a main routine that loops infinitely. By changing bool variables using keyboard
I have some variables I want to change via GUI. The variables are of
I am using VS 2005 I have about 10-12 variables of different types such

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.