c# 共用体 结构体 位段

lulu23260 2014-03-10 01:25:15
如题:如何实现位段分配呢? 数据类型怎么实现?
...全文
346 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lulu23260 2014-03-24
  • 打赏
  • 举报
回复
引用 7 楼 caozhy 的回复:
一样的思路。
按您的思路,很Ok!!谢谢
threenewbee 2014-03-11
  • 打赏
  • 举报
回复
你的代码根本不能编译。起码你给出合法的C++代码。否则只能给你思路了。
threenewbee 2014-03-11
  • 打赏
  • 举报
回复
一样的思路。
lulu23260 2014-03-10
  • 打赏
  • 举报
回复
想了想貌似不行,实现不了啊,要的是 union { int a; struct { uint b:1; int c:1; int d:2; } }
lulu23260 2014-03-10
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
没那么麻烦,C#有属性:
不错,谢谢 xiexe
threenewbee 2014-03-10
  • 打赏
  • 举报
回复
没那么麻烦,C#有属性: struct Test { private uint data = 0; public ushort High { get { return data / 65536; } set { data = data % 65536 + value * 65536; } } public ushort Low { get { return data % 65536; } set { data = data - data % 65536 + value; } } }
bigbaldy 2014-03-10
  • 打赏
  • 举报
回复
个人觉得最好的方法,摘自stackoverflow

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;
    }
  }
}
BenBenBears 2014-03-10
  • 打赏
  • 举报
回复

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧