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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:48:15+00:00 2026-06-13T12:48:15+00:00

I have a buffer with some data which come in different bit sizes (8

  • 0

I have a buffer with some data which come in different bit sizes (8 bits field, then 4 bits field, then a 9 bits field…).

I need to read it. It would be great if there where some library which allowed reading it using a pointer at bit level, and not a byte level.

Copying the buffer to a struct is not an option, because after researching I would need to use #pragma pack() or something similar, and would not be portable.

Any idea?

EDIT: I will try to explain the magnitude of my problem with an example:

field1: 8 bits --> ok, get first byte
field2: 6 bits --> ok, second byte, and a mask
field3: 4 bits --> gets harder, i have to get 2 bytes, apply 2 different masks, and compose
field4 
... 
field 15: 9 bits ---> No idea of how to do it with a loop to avoid writing manually every single case

And the only solution I can think of, is copying to an struct, pragma pack, and go. But I have been told in previous questions that it is not a good solution, because of portability. But I am willing to hear a different opinion if it rescues me.

  • 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-13T12:48:16+00:00Added an answer on June 13, 2026 at 12:48 pm

    Use bit manipulation:

    unsigned char[64] byte_data;
    size_t pos = 3; //any byte
    int value = 0; 
    int i = 0;
    int bits_to_read = 9;
    while (bits_to_read) {
        if (i > 8) {
          ++readPos;
          i = 0;
        }
        value |= byte_data[pos] & ( 255 >> (7-i) );
        ++i;
        --bits_to_read;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this buffer which I use for store some different types of data:
I have some library of classes, working with my data, which is being read
I have buffer/payload of integers where every 4 bytes is a field which i
So.. I have a buffer with MP3 data (If I would save this buffer
I have a buffer receiving data, which means the data are like 'stream' and
I have some data type which, if I were to use plain old C
I have a BufferedImage using an IndexColorModel to display some graphics data which uses
In our application we have some data structures which amongst other things contain a
I have an applet which displays some data using circles and lines. As the
I have a program which prints (by printf) to the stdout some data and

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.