LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting from Unsigned to Signed 16-bit Integer

Hi!

 

I'm a little rusty on the bit representation of negatively signed 16-bit integers.  I'm writing a LabVIEW driver for a piece of hardware that returns messages that contain the values of internal variables.  Each variable is a word long (16-bit), and should be interpreted as either signed (-32,768 to 32,767) or unsigned (0 to 65,535).  The manufacturer of the device has supplied me with a list of variables and how they should be interpreted (either signed or unsigned).

 

My LabVIEW VI interacts with a DLL to receive the Integer, and right now that value is being cast into an Unsigned 16-bit integer.

 

I wrote a simple LabVIEW program just to see how the convert to 16-bit Integer function handles input from an unsigned 16-bit integer.  That program is attached.  My question is this:

U16 value 1 is binary 0000 0000 0000 0001 and is I32 value 1 (of course)

U16 value 32,768 is binary 1000 0000 0000 0000 and is I16 value -32,768

 

If I recall correctly, for signed numbers if the MSB is 1, then the value is negative.  The magnitude of the value is calculated by inversing the remaining 15-bits.  This is apparently what LabVIEW is doing.  Is this conversion method considered universal?  Can I assume that the manufacturer intended the signed data coming from their device to be interpreted this way?

Is there another sign convention that is a possibility and I should probably contact the manufacturer?  The DLL and code was written about 15 years ago, and the manufacturer only has one person who worked on the project.  That person is probably tired of my questions by now.  I do not want to ask her a stupid question.  I'll ask you guys a stupid question instead ;).

 

Thanks!

-Nic

0 Kudos
Message 1 of 4
(22,844 Views)

Wikipedia has an entry on this. Yes, there are other representations, but 2's complement is the most common.

 

Tip: Instead of creating a Boolean array you can change the format of a numeric control/indicator to display multiple formats:

  • In the Format & Precision tab select "Advanced Editing Mode".
  • Hit Shift-Enter to create a new line
  • Enter the format. For example %016b will give 16 bits with zero-padding.
0 Kudos
Message 2 of 4
(22,832 Views)

The system you describe is called "two's-complement" and is the standard way for computers to interpret signed numbers, although your method of calculating it is off slightly (it's not quite as simple as inverting the remaining bits).

0 Kudos
Message 3 of 4
(22,822 Views)
Ahh I see.  It is inverted, and then "1" is added.
0 Kudos
Message 4 of 4
(22,816 Views)