Question:

Why is sign exntension a requirement in 2's complement? ?

by  |  earlier

0 LIKES UnLike

why can't we just put a 1 in the leading bit?

 Tags:

   Report

2 ANSWERS


  1. "When turning a two's-complement number with a certain number of bits into one with more bits (e.g., when copying from a 1 byte variable to a two byte variable), the sign bit must be repeated in all the extra bits and lower bits.

    Some processors have instructions to do this in a single instruction. On other processors a conditional must be used followed with code to set the relevant bits or bytes.

    Similarly, when a two's-complement number is shifted to the right, the sign bit must be maintained. However when shifted to the left, a 0 is shifted in. These rules preserve the common semantics that left shifts multiply the number by two and right shifts divide the number by two.

    Both shifting and doubling the precision are important for some multiplication algorithms. Note that unlike addition and subtraction, precision extension and right shifting are done differently for signed vs unsigned numbers."


  2. Strictly speaking, it is inaccurate to refer to the filling of 1's as extending the sign bit.

    When assigning a number in 2's complement to a longer datatype, the result is the logical consequence of converting the the original negative number to 2's comp in the longer format: there is still only one sign bit, no matter how many leading 1's in the number field there are.

    For example, in a single byte, the 2's complement of -30 is 1110 0010, the three leading 1's are not an extended sign bit: the sign bit is still 1 with the number field 110 0010. Casting this to integer type it becomes 1111 1111 1110 0010 , again still only one sign bit.

    Converting -30 to 16 bits requires the 16 bit representation of 30:

    0000 0000 0001 1110

    The by inverting each bit and adding 1 do we get the 2's complement

    1111 1111 1110 0001

                                +1

    -------------------------------

    1111 1111 1110 0010

    Only one sign bit: it is just a short cut to say "repeat the sign bit " to save time.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions