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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:20:55+00:00 2026-06-05T00:20:55+00:00

If I have 16 bits that represent 3 pairs of values, each 5 bits

  • 0

If I have 16 bits that represent 3 pairs of values, each 5 bits long, and one other 1 bit value, in exactly this order, is it safe to use a bitfield to describe this? Does ANSI C guarantee that the bits will be exactly in the order that I specify?

struct {
    unsigned v1 : 5;
    unsigned v2 : 5;
    unsigned v3 : 5;
    unsigned v4 : 1;
} test;

If not, is there any other data structure that I can use to represent this? Or should I just store two 8 bit chars and manipulate them programmatically to be assured of portability?

  • 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-05T00:20:56+00:00Added an answer on June 5, 2026 at 12:20 am

    The relevant quote I could find is 6.7.2.1(1), from C99:

    An implementation may allocate any addressable storage unit large enough to hold a bit-
    field. If enough space remains, a bit-field that immediately follows another bit-field in a
    structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

    I think that the unsigned isn’t actually the “underlying unit” type, but rather, it’s only a part of the bitfield declaration itself: T : n means “take n bits of type T“. The real question is whether the implementation is required to pick a “large” unit. For example, it could use three bytes by making the unit a char; one for v1, one for v2, and the last one for v3, v4. Or it could make the unit a 16-bit integer, in which case it would be required to use only one single unit.

    As you noted, though, the ordering of the bit fields within the unit is unspecified. The atomic unit of data in C is an address, and bitfields don’t have addresses. It’s guaranteed that the address of struct members increases in order of their declaration, but you cannot make such a statement about bitfields (only about their underlying units).

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

Sidebar

Related Questions

I have 16 bits. In each bit I can set some property and send
I have a bit field consisting of 64 bits: long bitfield = 0; I
I have a list of (64-bit) addresses that represent a stack frame, and I
I have an array of 128 booleans that represent bits. How can I convert
I have a set of 6 bits that represent a 7bit ASCII character. How
I have read that 'Normal' ARM instructions are fixed length - 32 bits. And
I have an RCP application that uses extension points to defined various pluggable bits
You have 2000 raw images, 52x52 pixels, RGB 24 bits, that correspond to the
I have a long number and I want to manipulate it bits in following
I have a list of bytes that represent raw samples read in from 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.