Bidang Bit dalam C#
Kod C# berikut menunjukkan cara mencipta medan bit dalam struktur, membolehkan akses bit menggunakan pengendali titik.
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(); } } }
Dalam kod ini:
Pendekatan ini membolehkan manipulasi medan bit yang mudah, menjadikannya amat berguna untuk mengendalikan format data berorientasikan bit.
Atas ialah kandungan terperinci Bagaimanakah Medan Bit Boleh Dilaksanakan dan Diakses dalam C# Menggunakan Atribut?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!