I am working on signed 2's complement for my fractions coefficients. Below code can be used for positive inputs. However for a negative 2's complement when I added LSB with 1, then an error pops. I think it is because of overflow. Now How can I convert negative coefficients using 2's complement for I and F of 3 and 9? and how to deal with overflow?
a = 0.7; % coefficient (actually for -0.7) n = 3; % number bits for integer part of your coefficient m = 9; % number bits for fraction part of your coefficient % binary number de2bi = fix(rem(a*pow2(-(n-1):m),2)); % signed 2's complement for negative numbers negadd = ; negadd = [negadd, zeros(1, length(de2bi) - length(negadd))]; flipadd = fliplr(negadd); magsig = fliplr(de2bi(bi2de(fliplr(de2bi))+bi2de(fliplr(flipadd)))); % back to binary b2d = magsig*pow2(n-1:-1:-m).';