Adding 2s compliment notation to a function
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 12 Feb. 2013
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
am 12 Feb. 2013
@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.
Walter Roberson
am 12 Feb. 2013
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
am 13 Feb. 2013
Darren
am 13 Feb. 2013
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Third-Party Cluster Configuration finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!