필터 지우기
필터 지우기

Convert 0.008 to binary?

조회 수: 26 (최근 30일)
Ivana
Ivana 2012년 9월 14일
댓글: Walter Roberson 2021년 3월 2일
Since the function dec2bin only works for integers, how can I convert 0.008 to binary?
  댓글 수: 4
Arun Kannan S
Arun Kannan S 2021년 3월 2일
what is signed?
Walter Roberson
Walter Roberson 2021년 3월 2일
signed is 0 if you want unsigned values and 1 if you want signed values. https://www.mathworks.com/help/fixedpoint/ref/embedded.fi.html#mw_0d34f1fa-0f13-43f0-b8b3-5878c069fa9f

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

채택된 답변

the cyclist
the cyclist 2012년 9월 14일
There are several submission in the File Exchange that do this. I have not used any of them, but here is one that is highly rated: http://www.mathworks.com/matlabcentral/fileexchange/13899-floating-number-conversion-to-binary-and-vice-versa
As Walter mentions, you need to take care about competing conventions.
  댓글 수: 2
Emilio Ortega
Emilio Ortega 2017년 10월 4일
Thanks! I really appreciate it
Duaa Basman
Duaa Basman 2019년 10월 1일
how i can convert array of fractional values to binary array values????

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 9월 14일
There is no one way to convert a decimal fraction to binary. There are a large number of competing conventions. You will need to specify exactly what representation you want to use.
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 10월 4일
The people who ask this question are often dealing with values that could potentially be negative. That leads into the question of how to represent negative binary values. Some of the competing conventions include:
  • two's complement. This is the most commonly used convention for integers. The negative of positive integer value P is found by taking the bitwise complement of P, and adding 1 to the result.
typecast(int16(-123),'uint16')
ans = 65413
For strictly negative value N, this is
TC16 = @(N) uint16( double(intmax('uint16')) + double(N) + 1 )
  • one's complement. This is not as commonly used except sometimes in some communications theory. The negative of positive integer value P is found by taking the bitwise complement of P, with nothing added to the result.
For strictly negative value N, this is
OC16 = @(N) uint16( double(intmax('uint16')) + double(N) )
  • separate sign. This is the most common arrangement for floating point representations. One bit is reserved to indicate whether the value is positive (typically the bit is 0 for this) or negative (typically the bit is 1 for this), but otherwise the positive representation of the integer is used.
For strictly negative value N, this is
SS16 = @(N) uint16(2^15) + uint16(abs(N))
However, notice the above assume fixed-width representation, which is not always what people are asking for when they are asking about converting decimal fractions to binary. It is not uncommon for people to be asking for some kind of minimal representation, that ends at the point where the remaining bits would all be 0. This requires more complex representations, because the representation has to somehow encode "the following is N bits wide" where N is a changing number depending on the value; or "the representation ends at this point". And that starts to get messy. It is not uncommon for fixed-width representations to be more efficient than variable-width representations, unless there are large differences in the frequency distributions that happen to favor the shorter items.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by