Real value to binary

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 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 11 日
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
b 2012 年 5 月 11 日
it will be 3 places after the decimal point.
so i think that i will have 10^3*25 equal size.
that is equal to
2^14 < 25000 < 2^15. so i will have 15 bits. ??
Walter Roberson
Walter Roberson 2012 年 5 月 11 日
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.

サインインしてコメントする。

 採用された回答

その他の回答 (3 件)

Honglei Chen
Honglei Chen 2012 年 5 月 11 日

0 投票

Are you asking things like dec2bin and bin2dec?
x = 20;
dec2bin(20,15)
Walter Roberson
Walter Roberson 2012 年 5 月 11 日

0 投票

dec2bin(round(Values * 2^10), 15) - '0'

5 件のコメント

b
b 2012 年 5 月 11 日
will i enter my number to "Values"?
And within the range [0,15]
??
Walter Roberson
Walter Roberson 2012 年 5 月 11 日
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
b 2012 年 5 月 11 日
Superb, my friend!
Thanks a lot :)
b
b 2012 年 5 月 11 日
Sorry for that but i want to ask another thing..
my range is between 0 and 25.
when i put e.g. 2.5 to this formula,this gives me wrong result
Walter Roberson
Walter Roberson 2012 年 5 月 11 日
If you go up to 25 then you need the 2^10 not 2^11

サインインしてコメントする。

b
b 2012 年 5 月 11 日

0 投票

Actually, if i put 2.154 as a real value within [0,25]
it should not give me 000000000000010. it represents for my range 0.001 something like that.

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by