0
\$\begingroup\$

If there's a number, say '1101,' how am I supposed to read it?

Should I consider it an unsigned number and read it as '13' or consider it a signed number and read it '-5' or consider it a 2's complement number and read it as '-3?'

| improve this question | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Signed and unsigned numbers are just two possibilities for what the 'meaning' could be. Other possibilities could be a character, one of a user defined enum, and almost anything else! Checkout the Ariane 5 launch disaster, what happens when a 'number' and the field assigned for representing it don't tally. \$\endgroup\$ – Neil_UK Jun 9 '19 at 9:47
3
\$\begingroup\$

enter image description here

LEWIS CARROLL (Charles L. Dodgson), Through the Looking-Glass, chapter 6, p. 205 (1934). First published in 1872.

I your case you are Humpty Dumpty and can decide what you want a digital word to mean. 4-bit logic would be rather unusual these days so let's use 8-bit for our example. 8 bits gives us 28 = 256 combinations including zero. Generally these would be used as follows:

Type                 Range
-------------------- ----------
Unsigned integer:       0 - 255
Signed integer:      -128 - 127
BCD:                   00 -  99

You also mention the possibility of using the first bit as sign and the remainder as the absolute value. This doesn't work out so well as the logic gets complicated.

  • There are two values for zero, 00000000 and 10000000.
  • The count sequence has to reverse when you cross zero.

In comparison the standard two's compliment handles the negative transition quite simply by rolling over from 00000000 (0) to 11111111 (-1), etc., in much the same way that the old mechanical mileometers in cars would roll over if you ran them backwards or forwards for long enough.

Some programming languages force you to define the data type before use. This cab be a good thing as it forces the user to consider exactly what they want and how much memory to set aside for each variable. Others are more flexible and adapt on the fly.

Lewis Carroll was a mathematician and would have appreciated the possibilities that this gives us.

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ Yeah, I got it. Two's complement way too efficient. \$\endgroup\$ – Evan Pk Jun 9 '19 at 10:08
2
\$\begingroup\$

How to interpret it depends on the context, and what operations are done to it. Most of the time with integer numbers, '1101' is either 13 (unsigned) or '-3' (signed in 2's complement), but extremely rarely, if ever in computers, as '-5' (signed in 1's complement notation, i.e. sign-magnitude).

It is because, if you take '1101' (13 or -3 depending on how you read it), if you add 3, or '0011' to it, you get zero, '0000'. If you take 13 '1101' and only add 2 to it, you get '1111', '15' or '-1'. It is up to the user that writes code (high level or assembly code) to tell the compiler which instructions to use when comparing numbers, are they signed or unsigned integers.

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ oh wow, thanks. All the time I thought only one thing is correct but there's nothing wrong. Both things are right. But I think we normally work with 2's complement signed numbers because there's no overflow condition so it's more efficient. \$\endgroup\$ – Evan Pk Jun 9 '19 at 10:06
  • \$\begingroup\$ The overflow is indicated by the 'carry' bit and this is how, for example, 16-bit counting or addition was done on an 8-bit processor: add the low bytes, if the carry is set then increment the high byte and then add the high bytes. If the carry is set again then you have to handle the overflow. \$\endgroup\$ – Transistor Jun 9 '19 at 12:30

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.