Hey I want to write a struct like the XNA Color struct or the SurfaceFormat.Bgra4444 struct that holds 2 nibbles in a 8 bit byte.
This is what I have so far…
/// <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;}
}
As you can see my propertys and constructors are not implimented. As this is the part I am having trouble with.
Thanks for any help you can provide.
Basically, you just need to keep in mind what is the high and low nibbles. The low nibble can be obtained just by masking with binary 1111 (decimal 15). The high nibble can be obtained (since
byteis unsigned) by right-shifting by 4. The rest is just bit-math.I can’t, however, help you with:
because that is 64 bits, and you want to fit it into 8. You’d need to be a lot more specific about what you want to do with those values.