In the context of signed numbers, "sign and magnitude" is a method of representing numbers in binary where the most significant bit (MSB)most significan bits, indicates the sign (0 for positive, 1 for negative), and the remaining bits represent the magnitude (absolute value) of the number.
Sign Bit:
The MSB is used to denote whether the number is positive or negative. A 0 in the MSB means the number is positive, while a 1 means it's negative.
Magnitude:
The remaining bits after the sign bit represent the absolute value (magnitude) of the number.
Example:
Let's consider a 4-bit signed number.
0101 represents +5 (sign bit is 0, magnitude is 101 which is 5 in decimal).
1101 represents -5 (sign bit is 1, magnitude is 101 which is 5 in decimal).
**Limitations:**
Sign and magnitude representation has some drawbacks:
Two Representations of Zero:
Both 0000 and 1000 represent zero, which can be problematic for certain computations.
Complex Arithmetic:
Arithmetic operations (addition, subtraction) are more complex than with other methods like two's complement.
Non-Optimal Range:
The range of numbers that can be represented in sign and magnitude is slightly less than in two's complement with the same number of bits.
**ONE'S COMPLEMENT REPRESENTATION**
Given a binary number s represented as a string. The task is to return its 1's complement and 2's complement in form of an array as [onesComplement, twosComplement].
The 1's complement of a binary number is obtained by flipping all its bits. 0 becomes 1, and 1 becomes 0. Positive numbers remain unchanged whereas negative numbers are represented by taking the 1's complement of their positive counterparts.
For example, in 8-bit notation:
+9 is represented as 00001001.
-9 is represented as 11110110, which is the 1's complement of 00001001.
**TWO'S COMPLEMENT REPRESENTATION**
The 2's complement of a binary number is obtained by finding the 1's complement (flipping all bits) and then adding 1 to the result. In 2's complement representation, the Most Significant Bit (MSB) represents the sign. A 0 indicates a positive number, while a 1 indicates a negative number. The remaining bits represent the magnitude.
Positive numbers are represented the same way as in 1's complement and sign-bit representation. Negative numbers are obtained by taking the 2's complement of their positive counterparts.
Negative Number, (-11)
to fine negative integer with two's complement represntation is as follow
Let's go through the steps to represent -11 in two's complement:
1. Get the binary representation of 11:
11 in binary is 00001011.
2. Invert the bits: where 0's flips to 1's and 1's to 0's
Flipping the bits, we get 11110100.
3. Add 1:
Adding 1 to 11110100, we get 11110101.
So, -11 in 8-bit two's complement representation is 11110101.
**In summary:**
Sign and magnitude is a simple method for representing signed integers, but it has limitations compared to two's complement due to its two representations of zero and complex arithmetic operations