Two's complement

Mathematical operation on binary numbers, and a number representation based on this operation / From Wikipedia, the free encyclopedia

Two's complement is a mathematical operation to reversibly convert a positive binary number into a negative binary number with equivalent (but negative) value, using the binary digit with the greatest place value (the leftmost bit in big-endian numbers, rightmost bit in little-endian numbers) to indicate whether the binary number is positive or negative (the sign). It is used in computer science as the most common method of representing signed (positive, negative, and zero) integers on computers,[1] and more generally, fixed point binary values. When the most significant bit is a one, the number is signed as negative. .

Two's complement is executed by 1) inverting (i.e. flipping) all bits, then 2) adding a place value of 1 to the inverted number. For example, say the number +6 is of interest. +6 in binary is 0110 (the leftmost MSB is needed for the sign; positive 6 is not 110 because it would be interpreted as -2). Step one is to flip all bits, yielding 1001. Step two is to add the place value one to the flipped number, which yields 1010. To verify that 1010 indeed has a value of −6, remember that two's complement makes the most significant bit represent a negative place value, then add the place values: 1010 = 1×(−23)+0×(22)+1×(21)+0×(20) = 1(−8) + 0 + 1(2) + 0 = −6.