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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:29:42+00:00 2026-05-27T06:29:42+00:00

I am writing a data packer for my game. The idea is to write

  • 0

I am writing a data packer for my game. The idea is to write something that can pack attributes, stream data over the network and other areas where data packing can be desirable. I have used a version of http://cplusplus.com/articles/zb07M4Gy/ to achieve part of my goal. My code looks like:

namespace detail
{
    // Determine whether _Type is signed or unsigned.
    // _Result is then either a signed or unsigned char
    template < typename _Type >
    struct sign
    {
        typedef typename std::_If< std::tr1::is_unsigned< _Type >::value, unsigned char, signed char >::_Type _Result;
    };
}

// Template for packing and unpacking data
// at bit and byte level
template < typename _Type >
struct packer
{
    typedef typename _Type  value_type;

    static const int num_bytes = sizeof(value_type);
    static const int num_bits  = num_bytes * CHAR_BIT;

    union
    {
        value_type packed_value;
        typename detail::sign< value_type >::_Result byte[num_bytes];
    };
};

My question is, how can I extend this code to allow access to the individual bits without bit shifting? I cannot use std::bitset since it has a non-trivial constructor and an array of bool would result in allocating a byte for each entry instead of a bit.

Ideally I would like to avoid bit setting/checking macros (although, it does seem my hands may be tied). There are portability and endian issues, but those aside, is there a way to achieve my goal?

An idea I had was to use something like:

template< int _NumBits >
struct bit_field
{
    bit_field< _NumBits - 1 > the_other_bits;

    int value : 1;

    int operator[](const int in_index)
    {
        return reinterpret_cast< int* >(this)[index];
    }
};

template<  >
struct bit_field< 0 >
{
    int value : 1;

    int operator[](const int in_index)
    {
        return reinterpret_cast< int* >(this)[index];
    }
};

Which would recursively build an array. However the problem with this (obviously) is alignment, which for this struct is 4bytes in MSVC10 giving the packer struct a total of 132 bytes, which is obviously not ideal when working with a 4 byte integer…

Any suggestions (including “man up and use bit shifting”…) are welcome.

  • 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-27T06:29:42+00:00Added an answer on May 27, 2026 at 6:29 am

    You can use bitfields:

    struct byte
    {
        bool bit0 : 1;
        bool bit1 : 1;
        bool bit2 : 1;
        bool bit3 : 1;
        bool bit4 : 1;
        bool bit5 : 1;
        bool bit6 : 1;
        bool bit7 : 1;
    };
    

    and then in your union

    union something
    {
        byte bytes[num_of_bytes];
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some C++ code that will have to send data over TCP/IP. I
I am writing an application that interprets a time delayed stream of data. The
I am writing a Silverlight application that will be both reading and writing data
I'm writing some data acquisition software and need a gui plotting library that is
I am writing a data conversion in PL/SQL that processes data and loads it
Background: I'm writing a data provider that SSAS will use to get data from
I am writing the data to the output browser using Response.write(some byte arrary) Response.ContentType
I am working on writing a high-performance data retrieval/storage class for my application that
I am writing a program that reads data packets from a file, and assigns
I'm writing a small (C#) client application that sends data using a TCP/IP connection

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.