繁体   English   中英

在C#中使用Nibbles

[英]Working with Nibbles in C#

嘿,我想编写一个结构,例如XNA Color结构或SurfaceFormat.Bgra4444结构,该结构在8位字节中包含2个半字节。

这是我到目前为止所拥有的...

/// <summary>
/// Packed byte containing two 4bit values
/// </summary>
public struct Nibble2 : IEquatable<Nibble2>
{
    private byte packedValue;

    public byte X
    {
        get { return 0; }
        set { }
    }

    public byte Y
    {
        get { return 0; }
        set { }
    }

    /// <summary>
    /// Creates an instance of this object.
    /// </summary>
    /// <param name="x">Initial value for the x component.</param>
    /// <param name="y">Initial value for the y component.</param>
    public Nibble2(float x, float y)
    {
        packedValue = 0;
    }

    /// <summary>
    /// Creates an instance of this object.
    /// </summary>
    /// <param name="vector">Input value for both components.</param>
    public Nibble2(Vector2 vector)
    {
        packedValue = 0;
    }

    public static bool operator ==(Nibble2 a, Nibble2 b) { return a.packedValue == b.packedValue; }
    public static bool operator !=(Nibble2 a, Nibble2 b) { return a.packedValue != b.packedValue; }

    public override string ToString()
    { return packedValue.ToString("X : " + X + " Y : " + Y, CultureInfo.InvariantCulture); }

    public override int GetHashCode()
    {return packedValue;}

    public override bool Equals(object obj)
    {
        if (obj is Nibble2)
            return Equals((Nibble2)obj);
        return false;
    }

    public bool Equals(Nibble2 other)
    {return packedValue == other.packedValue;}
}

如您所见,没有隐含我的属性和构造函数。 因为这是我遇到的麻烦。

感谢您的任何帮助,您可以提供。

基本上,您只需要记住什么是高字节和低字节。 只需用二进制1111(十进制15)进行掩码即可获得低半字节。 右移4可获得高半字节(因为byte是无符号的)。其余的只是位运算。

// assume x is the low nibble
public byte X
{
    get { return (byte)(packedValue & 15); }
    set { packedValue = (packedValue & 240) | (value & 15); }
}

// assume y is the high nibble
public byte Y
{
    get { return (byte) (packedValue >> 4); }
    set { packedValue = (value << 4) | (packedValue & 15); }
}

但是,我无法为您提供帮助:

public Nibble2(float x, float y)
{
    packedValue = 0;
}

因为这是64位,并希望将其融入8.你需要很多更具体的了解你想使用这些值做什么。

考虑以下字节值:

10101100 

要读取字节的高位,应将字节向右移动:

10101100 (original)
01010110 (shifted one bit)
00101011  
00010101 
00001010 (shifted four bits)

您可以使用return (byte)(packedValue >> 4);来移动字节return (byte)(packedValue >> 4);

要仅读取低位,只需使用AND运算就消除高位:

10101100 
00001111 AND
--------
00001100

您可以使用return (byte)(packedValue & 0xf);对值执行此AND操作return (byte)(packedValue & 0xf);

可以通过清除目标半字节,然后简单地添加输入值(如果设置高半字节,则向左移)来设置这些值:

packedValue = (byte)((packedValue & 0xf0) + (lowNibble & 0xf));
packedValue = (byte)((packedValue & 0xf) + (highNibble << 4));

如果您将输入值的字节填充为00001111 ,即十进制的15。 您保留要保存的零件。 然后,您必须将Nibble2的左侧部分左移4个字节,以存储在packedValue字节中。

private byte x = 0;
private byte y = 0;


public byte X
{
    get { return x; }
    set { x = value}
}

public byte Y
{
    get { return y; }
    set { y = value }
}

private byte packedValue 
{
    get { return (x & 15 << 4) | (y & 15); }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM