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

  • Home
  • SEARCH
  • 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 35815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:17:42+00:00 2026-05-10T14:17:42+00:00

I have a structure which I need to populate and write to disk (several

  • 0

I have a structure which I need to populate and write to disk (several actually).

An example is:

byte-6     bit0 - original_or_copy   bit1 - copyright   bit2 - data_alignment_indicator   bit3 - PES_priority   bit4-bit5 - PES_scrambling control.   bit6-bit7 - reserved   

In C I might do something like the following:

struct PESHeader  {     unsigned reserved:2;     unsigned scrambling_control:2;     unsigned priority:1;     unsigned data_alignment_indicator:1;     unsigned copyright:1;     unsigned original_or_copy:1; }; 

Is there any way to do this in C# that would enable me to access the bits using the struct dereferencing dot operator?

For a couple of structures, I can just do bit shifting wrapped in an accessor function.

I have loads of structures to handle in this way, so I’m looking for something that’s easier to read and quicker to write.

  • 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. 2026-05-10T14:17:43+00:00Added an answer on May 10, 2026 at 2:17 pm

    I’d probably knock together something using attributes, then a conversion class to convert suitably attributed structures to the bitfield primitives. Something like…

    using System;  namespace BitfieldTest {     [global::System.AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]     sealed class BitfieldLengthAttribute : Attribute     {         uint length;          public BitfieldLengthAttribute(uint length)         {             this.length = length;         }          public uint Length { get { return length; } }     }      static class PrimitiveConversion     {         public static long ToLong<T>(T t) where T : struct         {             long r = 0;             int offset = 0;              // For every field suitably attributed with a BitfieldLength             foreach (System.Reflection.FieldInfo f in t.GetType().GetFields())             {                 object[] attrs = f.GetCustomAttributes(typeof(BitfieldLengthAttribute), false);                 if (attrs.Length == 1)                 {                     uint fieldLength  = ((BitfieldLengthAttribute)attrs[0]).Length;                      // Calculate a bitmask of the desired length                     long mask = 0;                     for (int i = 0; i < fieldLength; i++)                         mask |= 1 << i;                      r |= ((UInt32)f.GetValue(t) & mask) << offset;                      offset += (int)fieldLength;                 }             }              return r;         }     }      struct PESHeader     {         [BitfieldLength(2)]         public uint reserved;         [BitfieldLength(2)]         public uint scrambling_control;         [BitfieldLength(1)]         public uint priority;         [BitfieldLength(1)]         public uint data_alignment_indicator;         [BitfieldLength(1)]         public uint copyright;         [BitfieldLength(1)]         public uint original_or_copy;     };      public class MainClass     {         public static void Main(string[] args)         {             PESHeader p = new PESHeader();              p.reserved = 3;             p.scrambling_control = 2;             p.data_alignment_indicator = 1;              long l = PrimitiveConversion.ToLong(p);               for (int i = 63; i >= 0; i--)             {                 Console.Write( ((l & (1l << i)) > 0) ? '1' : '0');             }              Console.WriteLine();              return;         }     } } 

    Which produces the expected …000101011. Of course, it needs more error checking and a slightly saner typing, but the concept is (I think) sound, reusable, and lets you knock out easily maintained structures by the dozen.

    adamw

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

Sidebar

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The more common term is Web bug: an invisible (i.e.… May 12, 2026 at 11:03 am
  • Editorial Team
    Editorial Team added an answer Have you looked at the comparison in the variant library… May 12, 2026 at 11:03 am
  • Editorial Team
    Editorial Team added an answer It's probably because data binding on the ListView happens AFTER… May 12, 2026 at 11:03 am

Related Questions

I'm a little stumped on this one. Anyone have any ideas? I'll try to
I have some code which utilizes parameterized queries to prevent against injection, but I
I would like to confirm that the following analysis is correct: I am building
I have to make a graphical user interface application using the language of my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.