I wrote two simple functions, to convert between decimal and two's complement integers:
function value = dec2tc(dec, N)
value = dec2bin(mod((dec),2^N),N);
end
function value = tc2dec(bin,N)
val = bin2dec(bin);
y = sign(2^(N-1)-val)*(2^(N-1)-abs(2^(N-1)-val));
if ((y == 0) && (val ~= 0))
value = -val;
else
value = y;
end
end
this is a very bad program,
Flaw 1) It doesnt work, e.g. heres what i get
dec2bit(0.3,5) = 00100, ok, then
dec2bit(-0.3,5) gives me just the 1's complement = 11011, but it should be 11100.