2's complement

E

eeh

Hi,

Could anyone give me sample code to calculate 2's complement of an
integer which is n (n<16) bits length?

Thanks!
 
P

Patricia Shanahan

Lucy said:
int a = 5;
int b = ~a;


That will give the 1's complement, the result of flipping
each bit. You can get the 2's complement in several ways but
the simplest is:

int b = -a;

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top