From Wikipedia:

A 32-bit register can store 2^32 different values. The range of integer values that can be stored in 32 bits depends on the integer representation used. With the two most common representations, the range is 0 through 4,294,967,295 (2^32 − 1) for representation as an (unsigned) binary number, and −2,147,483,648 (−2^31) through 2,147,483,647 (2^31 − 1) for representation as two's complement.

So if the integer representation used is "0 through 4,294,967,295 (2^32 − 1)", so does this mean the register cannot handle negative numbers?

From a similar standpoint, if the integer representation used is "−2,147,483,648 (−2^31) through 2,147,483,647 (2^31 − 1)", so does this mean that the register cannot handle numbers greater than 2,147,483,647?

  • $\begingroup$ I feel like we're going in circles. A 32-bit register is a collection of 32 bits. That's it. If you treat it as encoding an unsigned integer, it can store any integer from $0$ to $2^{32}-1$. If you treat it as encoding a signed integer using two's complement, it can store any integer from $-2^{31}$ to $2^{31}-1$. If you treat it as storing a fixed point unsigned with 2 bits after the dot, it can store any number from $0$ to $2^{30}-1/4$ in jumps of $1/4$. And so on. $\endgroup$ – Yuval Filmus Oct 22 '20 at 19:18
  • $\begingroup$ @Yuval Filmus What's the highest and lowest value it can represent? $\endgroup$ – Niraj Raut Oct 22 '20 at 19:26
  • $\begingroup$ Whatever you want. It depends on the representation. In IEEE 754 binary32, the answer is $(2 − 2^{−23}) × 2^{127}$. $\endgroup$ – Yuval Filmus Oct 22 '20 at 19:28
  • $\begingroup$ @Yuval Filmus Thanks, I will go through it. $\endgroup$ – Niraj Raut Oct 22 '20 at 19:30
  • $\begingroup$ @NirajRaut You need to realize the distinction between what can be represented and how many different things (assuming that different things should have different representations) can be represented. $2$ bits can represent $2^2=4$ different things. You can represent $5$. You can say that $00$ represents $5$, $01$ represents dog, $10$ represents -0.12 and $11$ represents $\star$. We cannot represent the integers from $0$ to $5$ with $2$ bits because we would need to repeat a code. $\endgroup$ – plop Oct 22 '20 at 20:30

The contents of a register don't have any inherent semantics.

Some instructions might assume certain semantics. For example, the x86 ADD instruction assumes that the registers represent integers, either unsigned or signed using two's complement. There are signed and unsigned versions in the x86 architecture for the multiplication instructions. Another unlikely place in which sign makes a difference is promotion instruction, in which a smaller register is assigned to a larger register – you want either zero extension (in the unsigned case) or sign extension (in the signed case, assuming two's complement).

What this all means is that certain the instruct set favors some interpretations by having instructions that assume them, but beyond that, the interpretation of the data stored in a register is completely up to the user. Moreover, in x86 at least, the same register can be used for both signed and unsigned operations. The register doesn't "know" which type of data it stores – it is completely up to the programmer. (One could imagine different architectures with different conventions.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.