Adding 2s compliment notation to a function
4 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Walter Roberson
on 13 Feb 2013
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 Comments
Walter Roberson
on 13 Feb 2013
Yes, sorry, mental mistake on my part. Should be the negatives of those, such as d - 2^24
More Answers (0)
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!