operation on binary numbers, and number representation based on this operation
Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. As with the ones' complement and sign-magnitude systems, … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Two's_complement
Two's complement - Wikipedia
4 days ago - + 1 + 1 =========== ===== 1010 0001 (two's complement) 161. Fundamentally, the system represents negative integers by counting backward and wrapping around. The boundary between positive and negative numbers is arbitrary, but by convention all negative numbers have a left-most bit (most significant bit) of one. Therefore, the most positive four-bit number is 0111 (7.) and the most negative is 1000 (−8.). Because of the use of the left-most bit as the sign bit, the absolute value of the most negative number (|−8.| = 8.) is too large to represent.
Discussions

Absolute value of a two's complement number
Hi, I'm trying to calculate the absolute value of a signed number (two's complement). Right now, I sign extend the input, and when... More on fpgarelated.com
🌐 fpgarelated.com
5
April 22, 2010
algorithm - How to compute the integer absolute value - Stack Overflow
Your solution for the absolute value might work, too. It's not what was asked about, though. In general, for more information on how bitwise operations work, a general learning resource on those might work better than trying to crawl through SO answers. 2019-10-13T16:42:01.223Z+00:00 ... @SparK No, because the mask is either 0 or all 1s, which is equivalent to -1 in twos-complement... More on stackoverflow.com
🌐 stackoverflow.com
August 21, 2012
c++ - Absolute value abs(x) using bitwise operators and Boolean logic - Stack Overflow
The author of the code expects two’s complement integers and an arithmetic right-shift, in which x >> 31 produces all zero bits if the sign bit of x is zero and all one bits if the sign bit is one. Thus, if x is positive or zero, y is zero, and x + y is x, so (x + y) ^ y is x, which is the absolute value ... More on stackoverflow.com
🌐 stackoverflow.com
Step by step, how to find the 2's complement representation of a number?
Wikipedia has a perfectly good explanation of it. Work it through by hand. It's not that tough and you'll learn a lot. More on reddit.com
🌐 r/learnprogramming
11
5
October 18, 2016
🌐
Google Groups
groups.google.com › g › comp.lang.vhdl › c › lYjatTFdCKY
Re: Absolute value of a two's complement number
On 4/23/2010 2:56 AM, Andy Rushton wrote: > Symon wrote: >> >> So, if I read numeric_std correctly (see below), in VHDL, for (say) >> four bit signed numbers, abs("1000"), aka abs(-8), equals zero. What >> am I missing? > > No, for a 4-bit signed, abs(-8) = -8! > > In 2's-complement notation, and therefore in numeric_std, the negation > of the most negative value is itself.
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › twosComplement
The Two's Complement
Consider the problem of representing both positive AND negative integers over a given range in terms of only ones and zeroes. A straight-forward approach would be to deal with the sign and the magnitude (or, the absolute value) separately. For example, suppose we have 8 bits with which to work.
🌐
The RISC-V Reader
fpgarelated.com › home › comp.arch.fpga › absolute value of a two's complement number
comp.arch.fpga | Absolute value of a two's complement number
April 22, 2010 - > So on a N bit processor with ... On most processors, twos complement arithmetic wraps on overflow. In that case, absolute value of the most negative value gives the same, most negative, value....
🌐
Hmc
fourier.eng.hmc.edu › e85_old › lectures › arithmetic › node5.html
Signed 2's Complement
Next: Signed-Complement Representation ... complement: When , it is represented in binary form, but the MSB must be 0. When , it is represented by 2's complement of its absolute value , but the MSB must be 1....
🌐
RIT
rit.edu › academicsuccesscenter › sites › rit.edu.academicsuccesscenter › files › documents › math-handouts › DM3_TwosComplement_BP_9_22_14.pdf pdf
Two's Complement
Step 1: Write the absolute value of the given number in binary form. Prefix this number with 0 ... Step 2: Take the complement of each bit by changing zeroes to ones and ones to zero. Step 3: Add 1 to your result. This is the two’s complement representation of the negative
Find elsewhere
🌐
Jonisalonen
jonisalonen.com › 2014 › the-twos-complement-anomaly
The Number With Negative Absolute Value – The Mindful Programmer
Recall that computer integers are actually defined modulo 2M, and in this case the correct result of -x = 128 is equivalent to -128 modulo 28. We did get the right result, it just has an unexpected interpretation. You’ll just have to get used to it: there are numbers with a negative absolute value and every time you see a call to abs() you should ask yourself if the argument can be the two’s complement anomaly, and what happens if it is.
Top answer
1 of 4
59

Assuming 32-bit words, as stated in the question:

For negative x, x >> 31 is implementation-defined in the C and C++ standards. The author of the code expects two’s complement integers and an arithmetic right-shift, in which x >> 31 produces all zero bits if the sign bit of x is zero and all one bits if the sign bit is one.

Thus, if x is positive or zero, y is zero, and x + y is x, so (x + y) ^ y is x, which is the absolute value of x.

If x is negative, y is all ones, which represents −1 in two’s complement. Then x + y is x - 1. Then XORing with all ones inverts all the bits. Inverting all the bits is equivalent to taking the two’s complement and subtracting one, and two’s complement is the method used to negate integers in two’s complement format. In other words, XORing q with all ones gives -q - 1. So x - 1 XORed with all ones produces -(x - 1) - 1 = -x + 1 - 1 = -x, which is the absolute value of x except when x is the minimum possible value for the format (−2,147,483,648 for 32-bit two’s complement), in which case the absolute value (2,147,483,648) is too large to represent, and the resulting bit pattern is just the original x.

2 of 4
18

This approach relies on many implementation specific behavior:

  1. It assumes that x is 32 bits wide. Though, you could fix this by x >> (sizeof(x) * CHAR_BIT - 1)
  2. It assumes that the machine uses two's complement representation.
  3. the right-shift operator copies the sign bit from left to right.

Example with 3 bits:

101 -> x = -3
111 -> x >> 2

101 + 111 = 100 -> x + y

100 XOR 111 -> 011 -> 3

This is not portable.

🌐
Northern Illinois University
faculty.cs.niu.edu › ~berezin › 463 › lec › data › numdata10b.html
2s complement.
Similar to 1's complement except value is adjusted when first stored. Machines that support separate 8, 16, and 32 bit numbers at hardware level Have distinct instructions or arguments for each number size to guarantee correct propagation of sign bit and overflow detection. Convert -117 to binary. Calculate the binary representation of the absolute value 117 MSB 58 r 1 LSB 117 - 64 * 1 = 53 29 r 0 53 - 32 * 1 = 21 14 r 1 21 - 16 * 1 = 5 7 r 0 5 - 8 * 0 = 5 3 r 1 5 - 4 * 1 = 1 1 r 1 1 - 2 * 0 = 1 0 r 1 MSB 1 - 1 * 1 = 0 1110101 64+32+16+4+1 = 117 (64*1+32*1+16*1+8*0+4*1+2*0+1*1) Left pad with zeros to recognized integer size, using 16-bit number.
🌐
Roanoke
cs.roanoke.edu › Fall2010 › CPSC120A › twoscomplement.html
Two's Complement Representation of Integers
Algorithm #2: The n-bit two's complement representation of -m (where m is a positive integer) is the base 2 representation of the positive integer 2n - m. In mathematical terms, this is the additive inverse of m modulo 2n. So, to calculate it: Find the base 2 representation of 2n - m. Algorithm ...
🌐
Academic Kids
academickids.com › encyclopedia › index.php › Two's_complement
Two's complement - Academic Kids
June 9, 2005 - If the leftmost bit is 0, the number is interpreted as a non-negative binary number. If the most significant (leftmost) bit is 1, the bits contain a negative number in two's complement form. To obtain the absolute value of the negative number, all the bits are inverted then 1 is added to the result.
🌐
Omni Calculator
omnicalculator.com › math › twos-complement
Two's Complement Calculator
January 30, 2026 - Here is the two's complement calculator (or 2's complement calculator), a fantastic tool that helps you find the opposite of any binary number and turn this two's complement to a decimal value. You have an opportunity to learn what the two's complement representation is and how to work with negative numbers in binary systems.
🌐
Techie Delight
techiedelight.com › home › binary › bit hacks – part 5 (find the absolute value of an integer without branching)
Bit Hacks – Part 5 (Find the absolute value of an integer without branching) | Techie Delight
September 17, 2025 - The idea is to use the expression (n+mask)^mask for computing the absolute value of n, where mask is n >> 31 (assuming 32–bit storage for integers). The mask n >> 31 will be evaluated to 0 for positive numbers and -1 for negative numbers.
Top answer
1 of 16
740

Two's complement is a clever way of storing integers so that common math problems are very simple to implement.

To understand, you have to think of the numbers in binary.

It basically says,

  • for zero, use all 0's.
  • for positive integers, start counting up, with a maximum of 2(number of bits - 1)-1.
  • for negative integers, do exactly the same thing, but switch the role of 0's and 1's and count down (so instead of starting with 0000, start with 1111 - that's the "complement" part).

Let's try it with a mini-byte of 4 bits (we'll call it a nibble - 1/2 a byte).

  • 0000 - zero
  • 0001 - one
  • 0010 - two
  • 0011 - three
  • 0100 to 0111 - four to seven

That's as far as we can go in positives. 23-1 = 7.

For negatives:

  • 1111 - negative one
  • 1110 - negative two
  • 1101 - negative three
  • 1100 to 1000 - negative four to negative eight

Note that you get one extra value for negatives (1000 = -8) that you don't for positives. This is because 0000 is used for zero. This can be considered as Number Line of computers.

Distinguishing between positive and negative numbers

Doing this, the first bit gets the role of the "sign" bit, as it can be used to distinguish between nonnegative and negative decimal values. If the most significant bit is 1, then the binary can be said to be negative, where as if the most significant bit (the leftmost) is 0, you can say the decimal value is nonnegative.

"Sign-magnitude" negative numbers just have the sign bit flipped of their positive counterparts, but this approach has to deal with interpreting 1000 (one 1 followed by all 0s) as "negative zero" which is confusing.

"Ones' complement" negative numbers are just the bit-complement of their positive counterparts, which also leads to a confusing "negative zero" with 1111 (all ones).

You will likely not have to deal with Ones' Complement or Sign-Magnitude integer representations unless you are working very close to the hardware.

2 of 16
397

I wonder if it could be explained any better than the Wikipedia article.

The basic problem that you are trying to solve with two's complement representation is the problem of storing negative integers.

First, consider an unsigned integer stored in 4 bits. You can have the following

0000 = 0
0001 = 1
0010 = 2
...
1111 = 15

These are unsigned because there is no indication of whether they are negative or positive.

Sign Magnitude and Excess Notation

To store negative numbers you can try a number of things. First, you can use sign magnitude notation which assigns the first bit as a sign bit to represent +/- and the remaining bits to represent the magnitude. So using 4 bits again and assuming that 1 means - and 0 means + then you have

0000 = +0
0001 = +1
0010 = +2
...
1000 = -0
1001 = -1
1111 = -7

So, you see the problem there? We have positive and negative 0. The bigger problem is adding and subtracting binary numbers. The circuits to add and subtract using sign magnitude will be very complex.

What is

0010
1001 +
----

?

Another system is excess notation. You can store negative numbers, you get rid of the two zeros problem but addition and subtraction remains difficult.

So along comes two's complement. Now you can store positive and negative integers and perform arithmetic with relative ease. There are a number of methods to convert a number into two's complement. Here's one.

Convert Decimal to Two's Complement

  1. Convert the number to binary (ignore the sign for now) e.g. 5 is 0101 and -5 is 0101

  2. If the number is a positive number then you are done. e.g. 5 is 0101 in binary using two's complement notation.

  3. If the number is negative then

    3.1 find the complement (invert 0's and 1's) e.g. -5 is 0101 so finding the complement is 1010

    3.2 Add 1 to the complement 1010 + 1 = 1011. Therefore, -5 in two's complement is 1011.

So, what if you wanted to do 2 + (-3) in binary? 2 + (-3) is -1. What would you have to do if you were using sign magnitude to add these numbers? 0010 + 1101 = ?

Using two's complement consider how easy it would be.

 2  =  0010
 -3 =  1101 +
 -------------
 -1 =  1111

Converting Two's Complement to Decimal

Converting 1111 to decimal:

  1. The number starts with 1, so it's negative, so we find the complement of 1111, which is 0000.

  2. Add 1 to 0000, and we obtain 0001.

  3. Convert 0001 to decimal, which is 1.

  4. Apply the sign = -1.

Tada!

🌐
Cornell University
cs.cornell.edu › ~tomf › notes › cps104 › twoscomp.html
Two's Complement
In the examples in this section, I do addition and subtraction in two's complement, but you'll notice that every time I do actual operations with binary numbers I am always adding. Suppose we want to add two numbers 69 and 12 together. If we're to use decimal, we see the sum is 81. But let's use binary instead, since that's what the computer uses. Now suppose we want to subtract 12 from 69. Now, 69 - 12 = 69 + (-12). To get the negative of 12 we take its binary representation, invert, and add one.