How to convert from sign-magnitude to two's complement

前端 未结 5 1463
有刺的猬
有刺的猬 2021-01-24 06:39

How would I convert from sign-magnitude to two\'s complement. I don\'t know where to start. Any help would be appreciated. I can only use the following operations:!,~,|,&,^,

5条回答
  •  灰色年华
    2021-01-24 07:06

    Signed Numbers are 8 bit quantities with the least significant 7 bits representing the magnitude and the most significant bit indicating the sign. 0 in this bit indicates the number is positive, and 1 indicates it is negative. There is no magnitude information in this 8th bit-just the sign.

    To convert a negative signed number to two's complement, first set the 8th bit to zero. Then invert all 8 bits. Finally add 1. An example follows:

    Signed Number:

    10001111

    set the 8th bit to zero: (use & operator)

    00001111

    invert all 8 bits: (use bitwise-complement operator)

    11110000

    finally, add 1; resulting in the final two's complement number: (use + operator)

    11110001

    If the 8th bit is 0, indicating that the signed number is positive, the number requires no conversion. It's two's complement representation is the same as the signed magnitude representation.

提交回复
热议问题