Adding 2s compliment notation to a function

I have a function as detailed below:
function [A B C] = elevation (d,p,t)
FC = 360;
d = hex2dec (d);
p = hex2dec (p);
t = hex2dec (t);
A = (d*0.5*FC/2^18);
B = (p*0.5*FC/2^15);
C = (t*0.5*FC/2^15);
If i call the function using [A B C] = elevation ('FFE38D', 'FC71', 'FC6D');
The above function would output decimal values 16769933, 64625, and 64621 respectively, however i would like the 2s compliment values -7282, -910 and -914. Preferably i would have the 2s compliment code wrote into the function rather than the command window.
Any help is much appreciated. Thanks in advance

댓글 수: 5

How long are your "words"? It looks like the first one might be 24 bits, and the second and third might be 16 bits?
James Tursa
James Tursa 2013년 2월 12일
@Darren: The conversion can be done with integer conversions (e.g., uint32, uint16, etc) followed by a typecast to a signed integer type (e.g., int32, int16), but it is critical that we know for sure where the sign bit is in your inputs, which is why Walter is asking about the word size of your inputs.
2s complement technically does not use a sign bit, but there is only one value that it makes a difference for (top bit set and the other bits all 0). None the less, the determination of whether a value is negative or not depends on the word length. For example, 0xF is merely 15, not negative, unless the word size is only 4 bits.
Darren
Darren 2013년 2월 13일
Yes Walter you are correct, the first word is 24 bits and the second and third are 16 bits.
So basically i'm looking for:
[A B C] = elevation ('FFE38D', 'FC71', 'FC6D'); % should equal negative values
[A B C] = elevation ('001C72', '038E', '0392'); % should equal positive values
In addition, i have looked searched through previous answers and tried the following:
function [A B C] = training2s(d,p,t)
FC = 360;
typecast(uint24(hex2dec(d),'int24'))
typecast(uint16(hex2dec(p,t),'int16'))
d = hex2dec (d);
p = hex2dec (p);
t = hex2dec (t);
A = (d*0.5*FC/2^18);
B = (p*0.5*FC/2^15);
C = (t*0.5*FC/2^15);
end
However the above code results in an error in the typecast lines.
Any help is much appreciated. Thanks

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2013년 2월 13일

0 개 추천

d = hex2dec (d);
if d > 2^23-1; d = 2^24 - d; end
p = hex2dec (p);
if p > 2^15-1; p = 2^16 - p; end
t = hex2dec (t);
if t > 2^15-1; t = 2^16 - t; end

댓글 수: 3

Darren
Darren 2013년 2월 13일
Thanks for your answer Walter. Using your code, the 3 outputs are all positive values when i would expect them to be positive? all 3 are +5 when i was expecting -5.
Yes, sorry, mental mistake on my part. Should be the negatives of those, such as d - 2^24
Darren
Darren 2013년 2월 14일
Thanks for all your help Walter! It works perfectly

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Third-Party Cluster Configuration에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by