Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

2's compliment in matlab

Status
Not open for further replies.

singhji0000

Junior Member level 3
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top