#Java Question

27 messages · Page 1 of 1 (latest)

peak scaffold
#

Hi I had this practise question. And I compltely dont understand what the question is asking, or the answer either. Does anyone get this?

static radishBOT
#

This post has been reserved for your question.

Hey @peak scaffold! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

sly kelp
#

I feel the choice of words isn't very precise. 0 and -0 are not different values at all. They are different expressions, but that's all.

#

I think you need to find out what the == operator does in Java.

#

And then you need to find out how the int datatype is being represented

peak scaffold
#

Isn't the == the equality operator so it checks if 2 values are equal

sly kelp
#

The question is just asking for which values of x the Java expression x == -x is true

peak scaffold
#

That can be anything right

#

Like 2 and -2 etc..

#

?

peak scaffold
sly kelp
#

You should check out how the int datatype is defined for Java. You would then see that it is a signed number, which means it supports negative numbers as well. And no, -2 ist not the same value as 2

#

I assume the whole question is just designed to teach you about the == operator and the int datatype

#
jshell> int x = 2
x ==> 2

jshell> x == -x
$2 ==> false

jshell> int x = -2147483648
x ==> -2147483648

jshell> x == -x
$4 ==> true
peak scaffold
#

But why is -2147482648 the same as -x ?

peak scaffold
#

please can someone explain

shy hazel
#

Java french ?

peak scaffold
#

Sorry dont get what you mean?

peak scaffold
shy hazel
#

I speek french

supple abyss
#

EDIT: My answer is wrong, but Java's int is a 32-bit signed two's complement integer. Look up https://en.wikipedia.org/wiki/Two's_complement to maybe get more insight

|| This is due to something I believe is called binary overflow. Using a signed integer, the most significant bit of it's binary representation is used to hold the +/- state. Imagine int is 3 bits long, for simplicity.

000 = 0
001 = 1
010 = 2
011 = 3
100 = -0
101 = -1
110 = -2
111 = -3

But if you subtract 1 from -3 (our min value), it would be -4 right? Well in this case, we do the operation and 111 becomes 1000. At the cpu level there is a carry flag which holds 1 in it's value. Since we are imagining int is 3 bits long, the 4th bit doesn't fit and is actually lost. So 1000 becomes 000.

As for the question and answer, it's poorly worded and poorly expressed. And also, since when do you negate a number by subtracting 1 instead of multiplying by -1? ||

supple abyss
warm onyx
#

nope, != 0

supple abyss
#

Yeah I think I got this wrong... Looking at two's compliment and maybe there are special rules for it.

#

Negating a two's complement number is simple: Invert all the bits and add one to the result. For example, negating 1111, we get 0000 + 1 = 1. Therefore, 1111 in binary must represent −1 in decimal. The system is useful in simplifying the implementation of arithmetic on computer hardware.

-100 => 011 + 1 => 100
-8 => 7+1 => -8

#

Ahh flipping all the bits in a twos-compliment number results in one less than the positive value. And when you add to MAX_VALUE, you get MIN_VALUE. So the +1 rolls back over into negative.