0
$\begingroup$

enter image description here

Imagine you load this data as a 4-byte word into a register R. What is the value in R on a big endian machine and what is it on a little endian machine if you interpret the bits as a signed integer encoded in 2s complement

On a big endian machine the register R would be something like this

enter image description here

not considering the last sentence, interpret the bits as a signed integer encoded in 2s complement, which I don't understand completely.

Should I convert all bits, 0s to 1s and 1s to 0s and then sum 1 to all the string or to sum 1 to each byte in the string?

Thanks.

$\endgroup$
1
  • 1
    $\begingroup$ Two's complement is a representation for signed integers. It doesn't involve just taking the negation of each bit. $\endgroup$ Commented Sep 29, 2014 at 15:08

1 Answer 1

1
$\begingroup$

To convert a 4-byte = 32-bit word to its negative you apply the two's complement operator. The operator is, as you said, invert all the bits, then add 1 to the word.

You can tell a 4-byte = 32-bit word represents a positive value if its most significant bit (the one all the way on the left) is "0". If the most significant bit is a "1" then the 32-bit word represents a negative value. Thus your example big-endian register, 01001101101101101111001111010111 must represent a positive number.

I assume you know how to convert a positive binary number to its decimal or hexadecimal equivalent. The hexadecimal equivalent of 01001101101101101111001111010111, for example, is 0x4db6f3d7.

If, on the other hand the leading bit had been a "1", as in, (just making a number up): 11001101101101101111001111010111 that represents a negative number. The negative of what positive number? The positive number that you will get if you apply the two's complement operator. Flip the bits and add 1. So the negative of 11001101101101101111001111010111 must be 00110010010010010000110000101000 + 1 = 00110010010010010000110000101001, which is hexadecimal 0x32490c29 so 11001101101101101111001111010111 must be -0x32490c29.

$\endgroup$
1
  • 2
    $\begingroup$ Bits are bits. So the register looks like your picture shows it. The question is asking "what number do these bits represent?" $\endgroup$ Commented Sep 29, 2014 at 16:07