LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

2's complement of double

Hello,

 

I need to convert a signed double to a two's complement hex string that is 4 bytes long. How do I go about doing this?

 

Thanks.

0 Kudos
Message 1 of 8
(5,296 Views)

A DBL will not fit in four bytes so there will be some loss (it needs 8 bytes).

 

Please explain what you actually want. How about byte order?

0 Kudos
Message 2 of 8
(5,286 Views)

Your question doesn't make sense on several fronts. First, a floating point value is saved in memory as a sign bit, an exponent, and mantissa part. So, it's not the same as an integer. You can have two's complement of the exponent and mantissa part, but not of the whole thing. Second, a signed double (DBL) is 8 bytes long, so perhaps you're thinking of an SGL? Third, what do you mean by a "two's complement hex string"? A numeric indicator can display the value in hex format if you just change the display properties. Or do you specifically want a string? And if it's a string, do you want the string to be the "binary string" in which case you'd set the display format of the string indicator to display hex, or do you want the string to display the actual hex numbers as characters (i.e., the character "1" and the letter "A")?

0 Kudos
Message 3 of 8
(5,277 Views)

I misspoke. I have a long number and I would like to get the two's complement of it. Basically if I take a number 16777216 in long and convert to HEX I get 0x01000000. If I convert -16777216 I get 0x7fffffff as the output. The problem is I am using signed numbers and my ICD of the hardware states that "for signed numbers, the most significant bit is the sign bit, so that i+1 bits for a twos complement integer." I have attached a picture of the VI that shows what I am doing.

 

ThanksUntitled.png

0 Kudos
Message 4 of 8
(5,267 Views)

next time try to crop the picture a bit. (even better, simply attach the VI or a snippet). Thanks!

 

Why is the "value" orange instead of blue?

For integers, scale by a power of two is just a bit shift. No need for fancy math.

Why would you possibly need to go via a formatted string intermediary and back?

 

What happens if you simply make "value" a I32 representation and typecast it to a string?

 

0 Kudos
Message 5 of 8
(5,258 Views)

I will try those. But honestly this was the quickest way for me to get it into the format that the hardware will accept. How do I take a negative number and get a two's complement of it? 

 

Thanks

0 Kudos
Message 6 of 8
(5,252 Views)

So are you saying that your code produces the correct result for all inputs?

 

You never said that but you were still talking about "problems" earlier. You just said what you are doing, but failed to say if it actually produces the correct result.

0 Kudos
Message 7 of 8
(5,245 Views)

@msimone wrote:

I will try those. But honestly this was the quickest way for me to get it into the format that the hardware will accept. How do I take a negative number and get a two's complement of it? 

 

Thanks


Two's complement is a format. It's a way of storing values. You're not converting anything. Numbers are already stored in two's complement format, so you do not need to do anything.

 

 

Perhaps you should read up on two's complement: http://en.wikipedia.org/wiki/Two%27s_complement#Calculating_two.27s_complement

0 Kudos
Message 8 of 8
(5,241 Views)