-3

I apologize if the question I’m raising is way too basic, but I really don’t understand this. And I my theoretical background is not software engineering.

Let’s say se are dealing with a Java int, which is 32 bit. So:

2 = 00000000 00000000 00000000 00000010

I understand Tilde of 2 is the bitwise of 2. So:

~2 = 11111111 11111111 11111111 11111101

For which I must find which positive number is the twos complement of this, which is 3, so ~2 = -3

But what I don’t get about all this is…

How does the machine differentiates when 11111111 11111111 11111111 11111101 means -3 And when does it mean -2billion?

I can’t understand this because it seems to violate the injective behavior between bits and numbers.

What am I getting wrong here?

LeoAlmDiniz
  • 75
  • 12
  • 3
    `11111111 11111111 11111111 11111101` as the value of a Java int never means ~2 Billion -- it always means -3. I'm not sure what you are asking. – tgdavies Mar 25 '23 at 02:29
  • 1
    Please edit your question to explain why you think it would ever mean -2billion. – Sören Mar 25 '23 at 02:34
  • 1
    -2 billion is represented entirely differently (`10001000110010100110110000000000`). – Unmitigated Mar 25 '23 at 02:35
  • 1
    Tle leftmost bit is the sign bit. 1 is negative. It is 2 billion if it is an unsigned int but Java does not have unsigned integers. – Roslan Amir Mar 25 '23 at 02:42
  • 1
    Java may not have an unsigned int data type, but it does have methods to support handling integers as if they were unsigned. Check out the methods having "unsigned" as part of the names in the [Integer API](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html) and the [Long API](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Long.html) – Old Dog Programmer Mar 25 '23 at 03:18
  • 1
    Does this answer your question? [What is “two's complement”?](https://stackoverflow.com/questions/1049722/what-is-twos-complement) – Karl Knechtel Mar 25 '23 at 03:46
  • Java `int` and its other integer types are *signed*. If the first bit is 1, it’s a negative number. If the first bit is 0, it’s a positive number or 0- – Ole V.V. Mar 25 '23 at 05:55

1 Answers1

3

As an unsigned 32-bit number, the value would be 4,294,967,293.

As mentioned, Java doesn't have a type that is a 32-bit unsigned integer, so for Java, the question is moot.

For other languages, and other machines, the concept does exist. You ask 'how does the machine differentiate' and in general, the answer is that it doesn't. It's up to the low-level programmer to decide whether 11111111 11111111 11111111 11111101 means -3 or 4,294,967,293. And thus you get to decide whether the result of adding 1 to 11111111 11111111 11111111 11111101 means 4,294,967,294 or -2. It's the same twos-complement addition.

There are some cases, say for a 'compare' instruction, where it matters whether the comparands are regarded as signed or unsigned. But (again, generally) this is handled by the programmer deciding to code 'signed compare' or 'unsigned compare'. For higher-level languages, this decision is implicit in the data type used: signed integer or unsigned integer.

Arfur Narf
  • 392
  • 1
  • 5