16 bit two's complement

  • Follow


Hi,
I am currently doing a project and am a novice to Matlab. I have a problem where i receive serial 'dataframes', each consisting of two 8-bit values (the high byte and the low byte), which have to be constructed to get a 16-bit value. This assembled value is already in two's complement and I need to get the original value. 
Any help is appreciated. Thanks in advance. 
0
Reply Thanushka 5/27/2010 9:38:03 PM

Thanushka wrote:

> I am currently doing a project and am a novice to Matlab. I have a 
> problem where i receive serial 'dataframes', each consisting of two 
> 8-bit values (the high byte and the low byte), which have to be 
> constructed to get a 16-bit value. This assembled value is already in 
> two's complement and I need to get the original value. Any help is 
> appreciated. Thanks in advance.

typecast([uint8(HighByte), uint8(LowByte)], 'int16')

On some systems you may need to reverse the high and low bytes.
0
Reply Walter 5/27/2010 9:42:38 PM


Walter Roberson <roberson@hushmail.com> wrote in message <htmp2a$48f$1@canopus.cc.umanitoba.ca>...
> Thanushka wrote:
> 
> > I am currently doing a project and am a novice to Matlab. I have a 
> > problem where i receive serial 'dataframes', each consisting of two 
> > 8-bit values (the high byte and the low byte), which have to be 
> > constructed to get a 16-bit value. This assembled value is already in 
> > two's complement and I need to get the original value. Any help is 
> > appreciated. Thanks in advance.
> 
> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
> 
> On some systems you may need to reverse the high and low bytes.

Thanks but by the original value i meant the value before taking the two's complement. My original post may not have been clear. Sorry. 
eg: LowByte = 0xFE  HighByte = 0xFF 
Answer should equal -2
0
Reply Thanushka 5/27/2010 9:59:06 PM

Thanushka wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message 
> <htmp2a$48f$1@canopus.cc.umanitoba.ca>...
>> Thanushka wrote:
>>
>> > I am currently doing a project and am a novice to Matlab. I have a > 
>> problem where i receive serial 'dataframes', each consisting of two > 
>> 8-bit values (the high byte and the low byte), which have to be > 
>> constructed to get a 16-bit value. This assembled value is already in 
>> > two's complement and I need to get the original value.

>> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
>>
>> On some systems you may need to reverse the high and low bytes.

> Thanks but by the original value i meant the value before taking the 
> two's complement. My original post may not have been clear. Sorry. eg: 
> LowByte = 0xFE  HighByte = 0xFF Answer should equal -2

As I said, you may need to reverse the bytes on some systems.

 >> typecast(uint8([hex2dec('FE'),hex2dec('FF')]),'int16')
ans =
      -2


Hmmm, looking further, it appears that Matlab is now only offered for "big 
endian" systems, so reversing the bytes is possibly needed on all current 
Matlab systems. Still, there are plenty of people around running older Matlab 
releases, possibly on systems that are little-endian .
0
Reply Walter 5/27/2010 10:12:27 PM

"Thanushka " <galazz442@gmail.com> wrote in message <htmpva$91s$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <htmp2a$48f$1@canopus.cc.umanitoba.ca>...
> > Thanushka wrote:
> > 
> > > I am currently doing a project and am a novice to Matlab. I have a 
> > > problem where i receive serial 'dataframes', each consisting of two 
> > > 8-bit values (the high byte and the low byte), which have to be 
> > > constructed to get a 16-bit value. This assembled value is already in 
> > > two's complement and I need to get the original value. Any help is 
> > > appreciated. Thanks in advance.
> > 
> > typecast([uint8(HighByte), uint8(LowByte)], 'int16')
> > 
> > On some systems you may need to reverse the high and low bytes.
> 
> Thanks but by the original value i meant the value before taking the two's complement. My original post may not have been clear. Sorry. 
> eg: LowByte = 0xFE  HighByte = 0xFF 
> Answer should equal -2

Using Walter's suggestion on a PC (Little Endian):

>> LowByte = hex2dec('FE')
LowByte =
   254
>> HighByte = hex2dec('FF')
HighByte =
   255
>> typecast([uint8(LowByte), uint8(HighByte)], 'int16')
ans =
     -2

This appears to give what you want if the inputs are double class. What class variable are your original values in? double or int8 or uint8? If they are int8 or uint8 already then you can dispense with the uint8 casts above and just use (on a Little Endian system):

typecast([LowByte, HighByte], 'int16')


James Tursa
0
Reply James 5/27/2010 10:23:04 PM

Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> 
> Hmmm, looking further, it appears that Matlab is now only offered for "big 
> endian" systems

Huh?  PC's are Little Endian.

James Tursa
0
Reply James 5/27/2010 10:25:06 PM

James Tursa wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message 
> <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
>>
>> Hmmm, looking further, it appears that Matlab is now only offered for 
>> "big endian" systems
> 
> Huh?  PC's are Little Endian.

I knew something looked wrong as I was typing it, sigh.
0
Reply Walter 5/27/2010 10:39:28 PM

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <htmrg2$fuo$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> > 
> > Hmmm, looking further, it appears that Matlab is now only offered for "big 
> > endian" systems
> 
> Huh?  PC's are Little Endian.
> 
> James Tursa


I'm running Matlab R2009a and this solution seems to be working fine. The data is stored in a matrix which seems to default to double data type. Thanks a lot to both replies. Much appreciated. 
0
Reply Thanushka 5/27/2010 10:43:06 PM

Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> Thanushka wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message 
> > <htmp2a$48f$1@canopus.cc.umanitoba.ca>...
> >> Thanushka wrote:
> >>
> >> > I am currently doing a project and am a novice to Matlab. I have a > 
> >> problem where i receive serial 'dataframes', each consisting of two > 
> >> 8-bit values (the high byte and the low byte), which have to be > 
> >> constructed to get a 16-bit value. This assembled value is already in 
> >> > two's complement and I need to get the original value.
> 
> >> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
> >>
> >> On some systems you may need to reverse the high and low bytes.
> 
> > Thanks but by the original value i meant the value before taking the 
> > two's complement. My original post may not have been clear. Sorry. eg: 
> > LowByte = 0xFE  HighByte = 0xFF Answer should equal -2
> 
> As I said, you may need to reverse the bytes on some systems.
> 
>  >> typecast(uint8([hex2dec('FE'),hex2dec('FF')]),'int16')
> ans =
>       -2
> 
> 
> Hmmm, looking further, it appears that Matlab is now only offered for "big 
> endian" systems, so reversing the bytes is possibly needed on all current 
> Matlab systems. Still, there are plenty of people around running older Matlab 
> releases, possibly on systems that are little-endian .

I'm running Matlab R2009a and this solution seems to be working fine. The data is stored in a matrix which seems to default to double data type. Thanks a lot to both replies. Much appreciated.
0
Reply Thanushka 5/27/2010 10:49:05 PM

"Thanushka " <galazz442@gmail.com> wrote in message <htmshq$lmk$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <htmrg2$fuo$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> > > 
> > > Hmmm, looking further, it appears that Matlab is now only offered for "big 
> > > endian" systems
> > 
> > Huh?  PC's are Little Endian.
> > 
> > James Tursa
> 
> 
> I'm running Matlab R2009a and this solution seems to be working fine. The data is stored in a matrix which seems to default to double data type. Thanks a lot to both replies. Much appreciated. 

Maybe unrelated but any idea how to implement this in C#?
Thanks in advance....
0
Reply Thanushka 5/28/2010 10:00:23 AM

"Thanushka " <galazz442@gmail.com> wrote in message <htmst1$d6j$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> > Thanushka wrote:
> > > Walter Roberson <roberson@hushmail.com> wrote in message 
> > > <htmp2a$48f$1@canopus.cc.umanitoba.ca>...
> > >> Thanushka wrote:
> > >>
> > >> > I am currently doing a project and am a novice to Matlab. I have a > 
> > >> problem where i receive serial 'dataframes', each consisting of two > 
> > >> 8-bit values (the high byte and the low byte), which have to be > 
> > >> constructed to get a 16-bit value. This assembled value is already in 
> > >> > two's complement and I need to get the original value.
> > 
> > >> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
> > >>
> > >> On some systems you may need to reverse the high and low bytes.
> > 
> > > Thanks but by the original value i meant the value before taking the 
> > > two's complement. My original post may not have been clear. Sorry. eg: 
> > > LowByte = 0xFE  HighByte = 0xFF Answer should equal -2
> > 
> > As I said, you may need to reverse the bytes on some systems.
> > 
> >  >> typecast(uint8([hex2dec('FE'),hex2dec('FF')]),'int16')
> > ans =
> >       -2
> > 
> > 
> > Hmmm, looking further, it appears that Matlab is now only offered for "big 
> > endian" systems, so reversing the bytes is possibly needed on all current 
> > Matlab systems. Still, there are plenty of people around running older Matlab 
> > releases, possibly on systems that are little-endian .
> 
> I'm running Matlab R2009a and this solution seems to be working fine. The data is stored in a matrix which seems to default to double data type. Thanks a lot to both replies. Much appreciated.

Maybe unrelated but any idea on how to implement this in C#?
Thanks in advance.
0
Reply Thanushka 5/28/2010 10:01:05 AM

"Thanushka " <galazz442@gmail.com> wrote in message <hto491$4ll$1@fred.mathworks.com>...
> "Thanushka " <galazz442@gmail.com> wrote in message <htmst1$d6j$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> > > Thanushka wrote:
> > > > Walter Roberson <roberson@hushmail.com> wrote in message 
> > > > <htmp2a$48f$1@canopus.cc.umanitoba.ca>...
> > > >> Thanushka wrote:
> > > >>
> > > >> > I am currently doing a project and am a novice to Matlab. I have a > 
> > > >> problem where i receive serial 'dataframes', each consisting of two > 
> > > >> 8-bit values (the high byte and the low byte), which have to be > 
> > > >> constructed to get a 16-bit value. This assembled value is already in 
> > > >> > two's complement and I need to get the original value.
> > > 
> > > >> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
> > > >>
> > > >> On some systems you may need to reverse the high and low bytes.
> > > 
> > > > Thanks but by the original value i meant the value before taking the 
> > > > two's complement. My original post may not have been clear. Sorry. eg: 
> > > > LowByte = 0xFE  HighByte = 0xFF Answer should equal -2
> > > 
> > > As I said, you may need to reverse the bytes on some systems.
> > > 
> > >  >> typecast(uint8([hex2dec('FE'),hex2dec('FF')]),'int16')
> > > ans =
> > >       -2
> > > 
> > > 
> > > Hmmm, looking further, it appears that Matlab is now only offered for "big 
> > > endian" systems, so reversing the bytes is possibly needed on all current 
> > > Matlab systems. Still, there are plenty of people around running older Matlab 
> > > releases, possibly on systems that are little-endian .
> > 
> > I'm running Matlab R2009a and this solution seems to be working fine. The data is stored in a matrix which seems to default to double data type. Thanks a lot to both replies. Much appreciated.
> 
> Maybe unrelated but any idea on how to implement this in C#?
> Thanks in advance.

You may need to get your hands dirty and work on the bits directly.  Use shift and or operators to assemble your int from the two bytes.  Then subtract one and flip the bits.

Some nice person has explained it here: http://en.wikipedia.org/wiki/Two's_complement
0
Reply Steve 5/28/2010 10:35:04 AM

"Walter Roberson" <roberson@hushmail.com> wrote in message 
news:htmqqd$6vm$1@canopus.cc.umanitoba.ca...
> Thanushka wrote:
>> Walter Roberson <roberson@hushmail.com> wrote in message 
>> <htmp2a$48f$1@canopus.cc.umanitoba.ca>...
>>> Thanushka wrote:
>>>
>>> > I am currently doing a project and am a novice to Matlab. I have a >
>>> problem where i receive serial 'dataframes', each consisting of two > 
>>> 8-bit values (the high byte and the low byte), which have to be > 
>>> constructed to get a 16-bit value. This assembled value is already in
>>> > two's complement and I need to get the original value.
>
>>> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
>>>
>>> On some systems you may need to reverse the high and low bytes.
>
>> Thanks but by the original value i meant the value before taking the 
>> two's complement. My original post may not have been clear. Sorry. eg: 
>> LowByte = 0xFE  HighByte = 0xFF Answer should equal -2
>
> As I said, you may need to reverse the bytes on some systems.
>
> >> typecast(uint8([hex2dec('FE'),hex2dec('FF')]),'int16')
> ans =
>      -2
>
>
> Hmmm, looking further, it appears that Matlab is now only offered for "big 
> endian" systems, so reversing the bytes is possibly needed on all current 
> Matlab systems. Still, there are plenty of people around running older 
> Matlab releases, possibly on systems that are little-endian .

And following up on this, you can use the third output of the COMPUTER 
function to determine the endian-ness of the machine on which your code is 
running.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/computer.html

You can use this to decide whether or not to reverse the bytes ... although 
now that I look at it, the table indicates all the currently-supported 
platforms are little endian.  I guess some of the platforms that we used to 
support but no longer do were big endian.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 


0
Reply Steven 5/28/2010 2:15:13 PM

Thanushka wrote:

> Maybe unrelated but any idea how to implement this in C#?

I have not looked at C#. In C or C++, I would use a union, preferably 
with one of the data types of fixed width (C99, not supported in C89). 
Something like,

union {
   struct {
     uint_8 low_byte;
     uint_8 high_byte;
   } as_bytes;
   int_16 as_word;
} word_and_bytes;

word_and_bytes combineword;

combineword.as_bytes.low_byte = '\xfe';
combineword.as_bytes.high_byte = '\xff';
sprintf('\d\n', combineword.as_word);

0
Reply Walter 5/28/2010 3:16:41 PM

13 Replies
453 Views

(page loaded in 0.591 seconds)

Similiar Articles:


















7/25/2012 6:32:19 PM


Reply: