Real value to binary
Ältere Kommentare anzeigen
How can i convert my real value to binary digits?
For example; i have values within [0,25] range and 15 binary digits to represent a variable.(4 variables totally)
How can i do that and also reverse of that?
3 Kommentare
Walter Roberson
am 11 Mai 2012
Fixed point or floating point? And to confirm, the input values are not necessarily integer?
25 requires 5 bits; should we assume 5 bits of integer and 10 bits of fraction?
b
am 11 Mai 2012
Walter Roberson
am 11 Mai 2012
3 decimal places requires 10 bits to resolve (2^(-10) = 1/1024)
You get slightly better resolution if you use 2^10 than if you use 10^3. It depends though on whether resolution is your goal or if "3 decimal places" is your goal.
Akzeptierte Antwort
Weitere Antworten (3)
Honglei Chen
am 11 Mai 2012
Are you asking things like dec2bin and bin2dec?
x = 20;
dec2bin(20,15)
Walter Roberson
am 11 Mai 2012
dec2bin(round(Values * 2^10), 15) - '0'
5 Kommentare
b
am 11 Mai 2012
Walter Roberson
am 11 Mai 2012
Yes, enter you number in Values.
If your range is up to "just less than" 16, then
dec2bin(round(Values * 2^11), 15) - '0'
For 2.154 the result would be
001000100111011
The first 4 bits (0010) are the 2, and the last 11 bits approximate 0.154
b
am 11 Mai 2012
b
am 11 Mai 2012
Walter Roberson
am 11 Mai 2012
If you go up to 25 then you need the 2^10 not 2^11
b
am 11 Mai 2012
0 Stimmen
Kategorien
Mehr zu Data Type Conversion 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!