Binary: Plusses & Minuses (Why We Use Two's Complement) - Computerphile
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about signed integer overflow, negative values in unsigned or signed types, and what two's complement actually means.
Recommended by 6 people across 6 different months, 2015 to 2021.
What it is
A Computerphile explainer in which Professor Brailsford of the University of Nottingham describes how negative numbers are represented in binary. He walks through signed-number schemes such as sign-magnitude and one's complement, and shows why two's complement became the standard.
What people said
Strong rec NathanOliver · 2021-05 · Stack Overflow · on “How come 129, which is 8-bit number, is stored as -127 in signed char in c++”
The MSB is the sign bit, it's just that negative numbers user the opposite bit pattern of positive numbers. If they didn't, we wouldn't be able to represent all possible 256 possibilities. Here is a great video on the subject: [this video]
Recommends Quest Monger · 2015-07 · Stack Overflow · on “Binary arithmetic - addition with overflow”
Should have read my textbook again :) In 2's complement arithmetic the carryover is thrown away, versus 1's complement arithmetic, where carryover is carried back and added to the result. This video helped me understand - [this video]
Recommends user2644013 · 2015-09 · Stack Overflow · on “Strange Behavior when printing "all powers of 2 that fit in a long"”
When the final y *= 2 is performed y becomes a value larger than that which can be stored as a long. As longs are two's complement in java, where the most significant bit indicates if a number is positive or negative, when you make a long larger than its max value the most significant bit changes from a 0 to a 1, meaning the value of the long becomes negative so your loop will never terminate. There is a good video on two's complement on youtube [this video] for a better explanation.
Recommends willnx · 2016-03 · Stack Overflow · on “Two's complement overflow”
Try checking out this video [this video] - it's short (~15 minutes) and does a good job explaining Two's complement
Recommends ahcox · 2019-02 · Stack Overflow · on “What is “two's complement”?”
To bitwise complement a number is to flip all the bits in it. To two’s complement it, we flip all the bits and add one. Using 2’s complement representation for signed integers, we apply the 2’s complement operation to convert a positive number to its negative equivalent and vice versa. So using nibbles for an example, 0001 (1) becomes 1111 (-1) and applying the op again, returns to 0001 . The behaviour of the operation at zero is advantageous in giving a single representation for zero without special handling of positive and negative zeroes. 0000 complements to 1111 , which when 1 is added. overflows to 0000 , giving us one zero, rather than a positive and a negative one. A key advantage of
Recommends user2262111 · 2019-12 · Stack Overflow · on “Function of type unsigned int returns negative number”
If you want to learn more from my other comment, see Binary: Plusses & Minuses (Why We Use Two's Complement) - Computerphile by Computerphile on youtube. [this video]