# Sign Extension for 13 bit to 16 bit number

## Recommended Posts

I am reading a 13 bit signed number out of an ADXL345.  It is contained in a high byte and a low byte.  I joined the two bytes into a UINT16, but for some reason I can't seem to find a way to get this number converted properly into a INT16.

I was trying the logical shift but that doesn't take care of it.  After I shift the number to the left by 3 bits, is there something else I need to use to shift it back but fill in the sign bit?  I searched this forum and the internet, but I couldn't seem to find a definite solution that worked.

##### Share on other sites

I am reading a 13 bit signed number out of an ADXL345.  It is contained in a high byte and a low byte.  I joined the two bytes into a UINT16, but for some reason I can't seem to find a way to get this number converted properly into a INT16.

I was trying the logical shift but that doesn't take care of it.  After I shift the number to the left by 3 bits, is there something else I need to use to shift it back but fill in the sign bit?  I searched this forum and the internet, but I couldn't seem to find a definite solution that worked.

According to the spec

(13-bit) measurement at up to Â±16 g. Digital output data is formatted as 16-bit twos complement

13 bit is the resolution, not the data format.

If you want to get sense out of the raw 13 bit register, I think you will need to calculate it via the following formula if its not already done for you.

g = (register_value-4096) * 0.00390625

where the "register_value" is the UINT16 provided by your high and low bytes.

That should map the 13 bit register onto the +16 to -16 g range..

Edited by ShaunR

##### Share on other sites

Shaun's probably right, but in case you do need to sign-extend, one approach is this: http://forums.ni.com/t5/LabVIEW/24bit-hex-to-2-s-complement/m-p/1686050#M599007

##### Share on other sites

I was expecting the sign extension to work on the part but for some reason it isn't.  I have a call into Analog Devices to try to figure out why this isn't working right.  I was hoping to find a work around while I wait to see if Analog Devices comes up with a reason why it isn't working.  Ned thanks for your post.  It worked.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×