Home > Backend Development > C++ > How Can Bit Fields Be Implemented and Accessed in C# Using Attributes?

How Can Bit Fields Be Implemented and Accessed in C# Using Attributes?

DDD
Release: 2025-01-03 21:29:41
Original
982 people have browsed it

How Can Bit Fields Be Implemented and Accessed in C# Using Attributes?

Bit Fields in C#

The following C# code demonstrates how to create bit fields within a structure, enabling bit access using the dot operator.

using System;

namespace BitfieldTest
{
    [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;

            foreach (var f in t.GetType().GetFields())
            {
                var attrs = f.GetCustomAttributes(typeof(BitfieldLengthAttribute), false);
                if (attrs.Length == 1)
                {
                    uint fieldLength = ((BitfieldLengthAttribute)attrs[0]).Length;
                    long mask = 0;
                    for (int i = 0; i < fieldLength; i++)
                        mask |= 1L << 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();
        }
    }
}
Copy after login

In this code:

  • Bit fields are defined using the [BitfieldLength(uint length)] attribute.
  • PrimitiveConversion class provides a ToLong method to convert the bit fields in a structure to a long integer.
  • PESHeader is a structure with six bit fields.
  • Main() function initializes the PESHeader structure and converts it to a long integer.
  • The bit representation of the long integer is then printed to the console.

This approach allows for easy manipulation of bit fields, making it particularly useful for handling bit-oriented data formats.

The above is the detailed content of How Can Bit Fields Be Implemented and Accessed in C# Using Attributes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template