2's compliment in matlab

Status
Not open for further replies.

singhji0000

Junior Member level 3
Joined
Apr 1, 2011
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,464
Hello all,

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?

thanks
 

Here's your code:
*******************
Code:
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;
********************

this is just one simple way of doing it.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…