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.

How to calculate 2's compelement in MATLAB?

Status
Not open for further replies.

testing test

Member level 3
Joined
Mar 3, 2010
Messages
65
Helped
5
Reputation
10
Reaction score
3
Trophy points
1,288
Activity points
1,656
Hi,

I want to find the 2's complement of -127 in MATLAB for 8 bits. I want to find 2's complement of a. So, I multiply it by -1 and takes its dec2bin:

Code:
a=-127
a=-1*a;
b=dec2bin(a,8);

Now b = '01111111'

I want to invert all the bits of b and add 1 to it. How can I do it?

Thank you.
 

I can sugest you perform in DEC radix, adding following step previous above code :

Code:
a = 256 - a + 1 ;

+++
 

Well, what I actually want is to convert
-127 to 129
-126 to 130
-125 to 131

And so on...

@andre_teprom: Your provided equation is not doing this.

@KerimF: Yes man it worked. Thanks!
 

These two lines worked fine:

a=-127;
a=256+a;

Thank you.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top