Two's complement: Difference between revisions

Content deleted Content added
streamlined opening paragraph, improved the logic
Line 1:
{{Short description|Mathematical operation on binary numbers, and a number representation based on this operation}}
 
'''Two's complement''' is the most common [[signed number representations|method of representing signed]] (positive, negative, and zero) [[Integer (computer science)|integers]] on computers,<ref>E.g. "Signed integers are two's complement binary values that can be used to represent both positive and negative integer values", Section 4.2.1 in ''Intel 64 and IA-32 Architectures Software Developer's Manual'', Volume 1: Basic Architecture, November 2006</ref> and more generally, [[Fixed-point arithmetic|fixed point binary]] values. Two'sAs complement useswith the [[Most significant bit|binary digit with the ''greatest'ones' valuecomplement]] asand the ''[[sign''-magnitude]] tosystems, indicatetwo's whethercomplement the binary number is positive or negative; whenuses the [[most significant bit]] isas the ''1sign'' theto numberindicate ispositive signed(0) asor negative and(1) whennumbers, theand mostnonnegative significantnumbers bitare isgiven ''0''their theunsigned numberrepresentation (6 is signed0110, aszero positive.is As0000); ahowever, resultin two's complement, non-negative numbers are represented as themselves:the 6[[bit iscomplement]] 0110,of zerotheir ismagnitude 0000,plus and1 (−6 is 1010). (theThe resultnumber of applyingbits in the [[bitwiserepresentation NOT]]by operatorbe toincreased 6by andpadding addingall 1).additional However,high whilebits theof numberpositive ofor binarynegative bitsnumbers iswith fixed1's throughoutor a0's, computationrespectively, itor isdecreased otherwiseby arbitraryremoving additional leading 1's or 0's.
 
Unlike the [[ones' complement]] scheme, the two's complement scheme has only one representation for zero, with room for one extra negative number (the range of a 4-bit number is -8 to +7). Furthermore, the same arithmetic implementations can be used on signed as well as unsigned integers<ref>
{{cite book
|first1=Alexandre |last1=Bergel
Line 15:
}}
</ref>
and differ only in the integer overflow situations, since the sum of representations of a positive number and its negative is 0 (with the carry bit set).
 
== Procedure ==