Ones' complement

Uses your system voice; voice and rate in settings.

Snapshot

≈ 8 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

The ones' complement of a binary number is the value obtained by inverting (flipping) all the bits in the number (that is, changing each 1 to a 0 and each 0 to a 1). The name "ones' complement" refers to the fact that such an inverted value, if added to the original, would always produce an "all ones" number (the term "complement" refers to such pairs of mutually additive inverse numbers, here in respect to a non-0 base number). This mathematical operation is primarily of interest in computer science, where it has varying effects depending on how a specific computer represents numbers. A ones' complement system or ones' complement arithmetic is a system in which negative numbers are represented by the inverse of the binary representations of their corresponding positive numbers. In such a system, a number is negated (converted from positive to negative or vice versa) by computing its ones' complement. An N-bit ones' complement numeral system can represent integers in the range −(2 N−1 −1) to 2 N−1 −1; in contrast, a two's complement numeral system can represent integers in the range −2 N−1 to 2 N−1 −1 (note that the range of negative integers is larger by one). It is one of three common representations for negative integers in binary computers, along with two's complement and sign-magnitude.…

The ones' complement of a binary number is the value obtained by inverting (flipping) all the bits in the number (that is, changing each 1 to a 0 and each 0 to a 1). The name "ones' complement" refers to the fact that such an inverted value, if added to the original, would always produce an "all ones" number (the term "complement" refers to such pairs of mutually additive inverse numbers, here in respect to a non-0 base number). This mathematical operation is primarily of interest in computer science, where it has varying effects depending on how a specific computer represents numbers. A ones' complement system or ones' complement arithmetic is a system in which negative numbers are represented by the inverse of the binary representations of their corresponding positive numbers. In such a system, a number is negated (converted from positive to negative or vice versa) by computing its ones' complement. An N-bit ones' complement numeral system can represent integers in the range −(2 N−1 −1) to 2 N−1 −1; in contrast, a two's complement numeral system can represent integers in the range −2 N−1 to 2 N−1 −1 (note that the range of negative integers is larger by one). It is one of three common representations for negative integers in binary computers, along with two's complement and sign-magnitude. The ones' complement binary numeral system is characterized by the bit complement of any integer value being the arithmetic negative of the value. That is, inverting all of the bits of a number (the logical complement) produces the same result as subtracting the value from 0. Many early computers, e.g., the UNIVAC 1101, CDC 160, CDC 1604, CDC 6600, LINC, DEC PDP-1, UNIVAC 1107, and their successors, used ones' complement arithmetic. Successors of the CDC 6600 continued to use ones' complement arithmetic until the late 1980s, and the descendants of the UNIVAC 1107 (the UNIVAC 1100/2200 and Unisys ClearPath Dorado series) continue to use ones' complement, but all modern computers use two's complement. (Current Unisys ClearPath Dorado machines emulate the ones' complement 36-bit instruction set on two's complement processors.) Programming language standards such as C++20 and C23 require two's complement and disallow other representations such as ones' complement. ==Number representation== Positive numbers are the same simple, binary system used by two's complement and sign-magnitude. Negative values are the bit complement of the corresponding positive value. The largest positive value is characterized by the sign (high-order) bit being off (0) and all other bits being on (1). The lowest negative value is characterized by the sign bit being 1, and all other bits being 0. The table below shows all possible values in a four-bit system, from −7 to +7. + − 0 0000 1111 — Both +0 and −0 return TRUE when tested for zero 1 0001 1110 — and FALSE when tested for non-zero. 2 0010 1101 3 0011 1100 4 0100 1011 5 0101 1010 6 0110 1001 7 0111 1000 ==Basics== Adding two values is straightforward. Simply align the values on the least significant bit and add, propagating any carry to the bit one position left. If the carry extends past the end of the word it is said to have "wrapped around", a condition called an "end-around carry". When this occurs, the bit must be added back in at the right-most bit. This phenomenon does not occur in two's complement arithmetic. 0001 0110 22 + 0000 0011 3 1111 0010 −13 —The correct result (6 − 19 = −13) It is easy to demonstrate that the bit complement of a positive value is the negative magnitude of the positive value. The computation of 19 + 3 produces the same result as 19 − (−3). Add 3 to 19. 0001 0011 19 + 0000 0011 3 0001 0110 22 —The correct result (19 − (−3) = 22). ==Negative zero== Negative zero is the condition where all bits in a signed word are 1. This follows the ones' complement rules that a value is negative when the left-most bit is 1, and that a negative number is the bit complement of the number's magnitude. The value also behaves as zero when computing. Adding or subtracting negative zero to/from another value produces the original value. Adding negative zero: 0001 0110 22 + 1111 1111 −0 0001 0110 22 The correct result (22 + (−0) = 22) Subtracting negative zero: 0001 0110 22 − 1111 1111 −0 0001 0110 22 The correct result (22 − (−0) = 22) Negative zero is easily produced in a ones' complement adder. Simply add the positive and negative of the same magnitude. 0001 0110 22 + 1110 1001 −22 ==Avoiding negative zero== The generation of negative zero becomes a non-issue if addition is achieved with a complementing subtractor. The first operand is passed to the subtract unmodified, the second operand is complemented, and the subtraction generates the correct result, avoiding negative zero. The previous example added 22 and −22 and produced −0. 0001 0110 22 0001 0110 22 1110 1001 −22 1110 1001 −22 + 1110 1001 −22 − 0001 0110 22 + 0001 0110 22 − 1110 1001 −22 =========== ==== but =========== ==== ; and =========== === but =========== === 1111 1111 −0 0000 0000 0 1111 1111 −0 0000 0000 0 "Corner cases" arise when one or both operands are zero and/or negative zero. 0001 0010 18 0001 0010 18 − 0000 0000 0 − 1111 1111 −0 =========== ==== =========== ==== 0001 0010 18 1 0001 0011 19 − 0000 0001 1 =========== ==== =========== ==== =========== ==== =========== ==== 0000 0000 0 1111 1111 −0 1111 1111 −0 1 1111 1110 −1 + 0000 0001 1 ================== 1111 1111 −0 0000 0000 0 0000 0000 0 1111 1111 −0 1111 1111 −0 − 1111 1111 −0 − 0000 0000 0 − 1111 1111 −0 − 0000 0000 0 =========== ==== =========== ==== =========== ==== =========== ==== 1 0000 0001 1 0000 0000 0 0000 0000 0 1111 1111 −0 − 0000 0001 1 ==See also== IEEE 754 — Standard for Floating-Point Arithmetic Signed number representations Two's complement ==References== th:การแทนจำนวนที่มีเครื่องหมาย#1’s Complement System

Text derived from https://en.wikipedia.org/w/index.php?oldid=1376984099 under CC BY-SA 4.0; modifications have been made.