24 bit signed number conversion
Author Message
24 bit signed number conversion

I have written code to convert a decimal value to a signed 24 bit binary
value (+/- 8,388,608 bin), the only problem is I am having problems
doing it for negative values.

Sat, 07 Apr 2001 03:00:00 GMT
24 bit signed number conversion

Quote:

> I have written code to convert a decimal value to a signed 24 bit binary
> value (+/- 8,388,608 bin), the only problem is I am having problems
> doing it for negative values.
> Any suggestions, thanks in advance

Surely you can just convert the number ignoring the sign to binary and
then invert each bit and add one to it (if you want 2's complement).

If you want 1's complement then just invert each bit.

If you just want explicit sign then just invert the first sign bit after
converting the positive number.
--
***************************************
**    EMail Sent By Peter Denton     **

**       Phone: (01274) 232559       **
***************************************
* "I tell you the truth, unless you   *
* change and become like little       *
* children, you will never enter the  *
* kingdom of heaven" - Matthew 18v3   *
***************************************

Sun, 08 Apr 2001 03:00:00 GMT
24 bit signed number conversion
Without seeing what you're doing now it's a little difficult to know what
you need to add, but if what you're doing is converting the decimal value to
a string of 1's and 0's, then all you need to do is cast the value into the
32-bit number space (a long integer) and mask off the upper 8 bits.  This
will turn it into an unsigned (technically, positive) value having the same
bit pattern as the signed 24-bit value.  This works whether the value is
negative or positive:

MyVal& = (CLng(My24bits) And &HFFFFFF) 'unsigned' long

Then just convert the lower 24 bits of that value to a binary string.
Ignore what the value appears to be... the bit pattern is the same and
that's all that matters.

--
Jim Mack
MicroDexterity, Inc

http://www.microdexterity.com

Quote:

>I have written code to convert a decimal value to a signed 24 bit binary
>value (+/- 8,388,608 bin), the only problem is I am having problems
>doing it for negative values.

Sun, 08 Apr 2001 03:00:00 GMT
24 bit signed number conversion

schreibt:

Quote:
>I have written code to convert a decimal value to a signed 24 bit binary
>value

When you store the value in a Long variable, it has 32 bits of precision, so
you must fill the exceeding 8 bits with the sign of the 24 bit value:

Dim l as Long
l = value
If l And &h800000 Then l=l Or &hFF000000 ' extend sign

DoDi

Tue, 10 Apr 2001 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages