signed nibble

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jan 7 18:53:54 UTC 2019


On Mon, Jan 07, 2019 at 05:23:19PM +0000, Michelle Long via Digitalmars-d-learn wrote:
> Is there any direct way to convert a signed nibble in to a signed byte
> with the same absolute value? Obviously I can do some bit comparisons
> but just curious if there is a very quick way.

Assuming you have the nibble stored in the lower bits of a ubyte:

	import std.stdio;
	byte nibSgnExt(ubyte nib)
	{
		return cast(byte)(nib << 4) >> 4;
	}
	void main() {
		writefln("%02X", nibSgnExt(0x0F));
		writefln("%02X", nibSgnExt(0x07));
	}


T

-- 
If it breaks, you get to keep both pieces. -- Software disclaimer notice


More information about the Digitalmars-d-learn mailing list