[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3739
  • Last Modified:

Function to calculate two's complement of a number in C++

I am new to C++ and would like sample code on how to create two's complement of a number.
0
HariniMadugula
Asked:
HariniMadugula
  • 3
1 Solution
 
x4uCommented:
have a look at this article to find out what the two's complement is and what it is used for http://en.wikipedia.org/wiki/Two's_complement

int number = 12345; // whatever you like
int twoscompl = ~number + 1; // this is the way as it is defined (negate all bits and add 1)
or
int twoscompl = -number;  // this is the trivial way to do the same thing in any programming language
0
 
x4uCommented:
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now