1

Lets say I work in a 32 bit machine. With reference to MSB (most significant bit) which is the sign bit, my question is how is it represented for this archietecture.

My understanding is that every byte has a sign value. So if this is the case then there would be four signed bits. Am I correct or missing something here. Thanks!

Shash
  • 4,160
  • 8
  • 43
  • 67
  • It depends: Is a byte signed or unsigned by default with your compiler? – Richard J. Ross III Jun 21 '12 at 12:24
  • Bytes don't have "signs". A byte is just a unit of data. It has no meaning beyond that. – Kerrek SB Jun 21 '12 at 12:34
  • See example of signed/unsigned interpretation [here](http://stackoverflow.com/questions/11123014/what-is-the-deal-with-assigning-an-unsigned-variable-to-a-signed-value/11123205#11123205) – wap26 Jun 21 '12 at 13:22

4 Answers4

5

Bytes are groups of bits. They don't have any inherent meaning, and neither do the bits that make them up. Meaning is only given if you e.g. choose to interpret these four bytes as a signed integer, and then the bits mean what they mean because the platform designers say so.

In practically all prevalent architectures, integers are represented using two's complement. This means that:

  1. There's only one sign bit for the whole value (not for every byte), but this is a de facto condition, simply because the platform designers chose to use two's complement -- not because there's something special about all MSBs in the universe.
  2. There's no sign bit at all for unsigned values (obviously).
Jon
  • 428,835
  • 81
  • 738
  • 806
3

The specific representation of integers in C is not specified by the standard. However, in practice, most (virtually all?) machines will use two's complement representation for negative numbers. Two's complement does not have a sign bit, per se, but the most significant bit of the integer will be 1 for negative numbers and 0 for nonnegative numbers,

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
0

On most architecture your 32-bits signed integers are stored in two's complement : there is only one "sign bit" (which is not really one strictly speaking).

strnk
  • 2,013
  • 18
  • 21
0

If you're wrking with signed integers, then the compiler treats groups of bytes as an entire number, and that group has one sign bit. In C terminoligy: when you use int16_t, two bytes have one sign bit. For int32_t, four bytes have one. For signed char, each signed char (=byte) has a sign bit.