Two's complement
From Just Solve the File Format Problem
(Difference between revisions)
(Created page with "{{FormatInfo |formattype=electronic |subcat=Binary Data }} '''Two's complement''' is the most common way to represent a signed integer using a fixed number of bits. The high ...") |
Dan Tobias (Talk | contribs) |
||
(4 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
{{FormatInfo | {{FormatInfo | ||
|formattype=electronic | |formattype=electronic | ||
− | |subcat= | + | |subcat=Elements of File Formats |
+ | |wikidata={{wikidata|Q232577}} | ||
}} | }} | ||
'''Two's complement''' is the most common way to represent a signed integer using a fixed number of bits. | '''Two's complement''' is the most common way to represent a signed integer using a fixed number of bits. | ||
− | The high bit | + | The high bit can be thought of as the ''sign bit'' (though this is arguable, as it represents more than just the sign). If the sign bit is 1, the number is negative. If it is 0, the number is positive or zero. Positive numbers use the standard base-2 representation. |
To convert a number N to −N, invert all the bits, then add 1, discarding any carry. This works for any number except the maximally negative number, which has no positive equivalent. | To convert a number N to −N, invert all the bits, then add 1, discarding any carry. This works for any number except the maximally negative number, which has no positive equivalent. | ||
Line 13: | Line 14: | ||
== Resources == | == Resources == | ||
* [[Wikipedia:Two%27s complement|Wikipedia article]] | * [[Wikipedia:Two%27s complement|Wikipedia article]] | ||
+ | |||
+ | [[Category:Number formats]] |
Latest revision as of 02:26, 29 May 2019
Two's complement is the most common way to represent a signed integer using a fixed number of bits.
The high bit can be thought of as the sign bit (though this is arguable, as it represents more than just the sign). If the sign bit is 1, the number is negative. If it is 0, the number is positive or zero. Positive numbers use the standard base-2 representation.
To convert a number N to −N, invert all the bits, then add 1, discarding any carry. This works for any number except the maximally negative number, which has no positive equivalent.
The term two's complement also means the operation described above.