i needed to find the 2's compliment. I am getting result from comparator in form of 0 and 1 and the result are getting stored in array
eg. x = [1 0 1 1 0 0 1 1] from LSB to MSB
how can i find the 2's compliment for this result stored in the array using matlab program?
clc
clear
% x = your array
x = [1 0 1 1 0 0 1 1];
for i=1:8
if(x(i))
y(i) = 0;
else y(i) = 1;
end
end
% y contains inverted number
c = y;
if(~c(1))
c(1) = 1;
else
for i=1:8
if(~c(i))
c(i) = 1;
c(1:i-1) = 0;
break;
end
end
end
% c contains 2's complement
clear i;