We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


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

Medium Priority
Last Modified: 2011-10-03
I am new to C++ and would like sample code on how to create two's complement of a number.
Watch Question

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)
int twoscompl = -number;  // this is the trivial way to do the same thing in any programming language

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.