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

Answers (4)

Negative binary strings do not exist. To represent negative in binary, you have to use an encoding scheme. The three common encoding schemes are
  1. separated sign, where a bit is reserved for saying whether the value is positive of negative. IEEE 754 format uses separated sign
  2. 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
  3. 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.)
Dear Walter, Thank you for the reply. Does anyone know how to fix the fix2dec() function? Regards Joe

1 Comment

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.

Sign in to comment.

Dear Walter,
I am just trying to get the example working.
In the header of "fix2dec()" the example is given with output.
% Example:
% >> fix2dec(['10011.10110'; ' 11.1 '; ' 0.101110 '])
%
% ans =
%
% -12.3125
% -0.5000
% 0.7188
Both the first and second part of the example fails.
Regards Joe

1 Comment

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.

Sign in to comment.

Dear Walter, Thank you. I see now that the function bitcmp() does not operate the same in newer versions of matlab. Is there a function to be able to convert twos complement binary to decimal. The format is two complement fix32_29 "sii.fffffffffffffffffffffffffffff". Regards Joe

2 Comments

Please do not create a new Answer for each response. Just click on the "Comment on this Answer" link.
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.

Sign in to comment.

Asked:

on 22 Mar 2018

Commented:

on 23 Mar 2018

Community Treasure Hunt

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

Start Hunting!