FIX2DEC fails for negative binary string
이전 댓글 표시
Dear Sir/Madam,
When fix2dec('10011.10110') is used with negative binary strings the following error occurs.
Any help fixing would be much appreciated.
Error using bitcmp
ASSUMEDTYPE must be an integer type name.
Error in twos2dec (line 47)
x(xneg) = -( bitcmp(x(xneg), nbits(xneg)) + 1 );
Error in fix2dec (line 56)
x = twos2dec(t) .* 2.^-frac;
Regards Joe
답변 (4개)
Walter Roberson
2018년 3월 22일
0 개 추천
Negative binary strings do not exist. To represent negative in binary, you have to use an encoding scheme. The three common encoding schemes are
- separated sign, where a bit is reserved for saying whether the value is positive of negative. IEEE 754 format uses separated sign
- one's complement, where each bit of the number is directly inverted. This is not common in practice but it is used in some contexts
- two's complement, in which you invert the bits and then add one. This is common for integer representation.
For one's complement and two's complement if you are using a fixed-point representation with a separator between integer and fraction, then you need to decide whether the fraction is also complemented or not (calculations work out easier if it is complemented.)
Joe Miller
2018년 3월 22일
0 개 추천
댓글 수: 1
Walter Roberson
2018년 3월 22일
There is no fix; the function is not broken. You are passing it invalid input.
It would be legitimate to ask for an enhancement to process encodings of negative values, but first you need to define exactly what the desired new encoding is.
Joe Miller
2018년 3월 22일
편집: Walter Roberson
2018년 3월 22일
댓글 수: 1
Walter Roberson
2018년 3월 22일
The bitcmp() functionality used was valid only until R2014a, and was removed in R2014b. There is no replacement MATLAB call for it.
The n bit two's complement for a value is 2^n-1 minus the value.
Joe Miller
2018년 3월 23일
0 개 추천
댓글 수: 2
Walter Roberson
2018년 3월 23일
Please do not create a new Answer for each response. Just click on the "Comment on this Answer" link.
Walter Roberson
2018년 3월 23일
The two's complement for an integer, V, that is n bits long is ((2^n-1)-V) . There is no MATLAB call for this except in the cases where n is 8 or 16 or 32 or 64.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!