signed nibble

H. S. Teoh hsteoh at
Mon Jan 7 18:56:17 UTC 2019

On Mon, Jan 07, 2019 at 06:42:13PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:
> On Monday, 7 January 2019 at 17:23:19 UTC, Michelle Long 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.
> byte b = nibble | ((nibble & 0x40)?0xF0:0);

This is equivalent to doing a bit comparison (implied by the ?
operator).  You can do it without a branch:

	cast(byte)(nibble << 4) >> 4

will use the natural sign extension of a (signed) byte to "stretch" the
upper bit.  It just takes 2-3 CPU instructions.


Written on the window of a clothing store: No shirt, no shoes, no service.

More information about the Digitalmars-d-learn mailing list