About Question enthuware.ocajp.i.v7.2.1159 :
Moderator: admin
Re: About Question enthuware.ocajp.i.v7.2.1159 :
I would like to add this post from stackoverflow.com
It helps to clarify the point of this question in my view.
http://stackoverflow.com/questions/1253 ... en-compare
It helps to clarify the point of this question in my view.
http://stackoverflow.com/questions/1253 ... en-compare
-
- Posts: 32
- Joined: Wed Aug 28, 2013 6:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Very interesting question - assumed that the answer would be an overflow (so got it wrong/right?). Remembered the min and max vals would be one out, but expected mathematical rules to take precedence (i.e. -(-x) = +x) and so would overflow.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Please let me know if I am wrong, but I think the explanation for this question is a little misleading when describing two's complement.
The second step is to add 1 but it shows adding 32 1's to the complemented number, which is not correct if I am not mistaken. In other words it is like the explanation is saying to add the MAX_VALUE in binary of the number to the complemented (everything switched to the opposite) binary value.
Regards,
Javanaut
The second step is to add 1 but it shows adding 32 1's to the complemented number, which is not correct if I am not mistaken. In other words it is like the explanation is saying to add the MAX_VALUE in binary of the number to the complemented (everything switched to the opposite) binary value.
Regards,
Javanaut
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
I don't see it adding 32 1's. It shows the number that you get after adding 1 to step 1.
Paul.
HTH,Step 1 (complement the bits of 1): ///11111111 11111111 11111111 11111110///
Step 2 (add 1 to step 1): ///11111111 11111111 11111111 11111111///.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Oh yeah, I just added it on paper in binary and adding one to the complemented bits in step one does produce 32 1's. I see what the explanation is saying now.
Thanks Paul.
Regards,
Javanaut
Thanks Paul.
Regards,
Javanaut
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Code: Select all
// java program by javanaut :D
public class Foo {
static public void main(String[] args) {
Integer x = 0b11111111_11111111_11111111_11111111;
System.out.println(x);
Integer x1 = Integer.MAX_VALUE;
System.out.println(x1);
System.out.println(Integer.toBinaryString(x));
System.out.println(Integer.toBinaryString(x1));
}
}
Code: Select all
-1
2147483647
11111111111111111111111111111111
1111111111111111111111111111111
I made this program to try and experiment with some of this - binary numbers, two's complement, etc. I noticed that the first toBinaryString() method in my program prints out one more 1 that the second call to this method. Can someone please explain?
The arguments to the toBinaryString() method are -1 in x and MAX_VALUE found in x1.
Regards,
Javanaut
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
That is because there is one more 1 in the binary representation of -1 than in MAX_VALUE. Please read how -1 is represented in a 32 bit int.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Paul,
Thank-you for the reply. How odd that MAX_VALUE has only 31 bits when it is an int that is a 32 bit data-type.
This part of Java is not really on the OCAJP7 exam though?
Thank-you again for helping me understand what I was missing with the output of my program.
Regards,
Javanaut
Thank-you for the reply. How odd that MAX_VALUE has only 31 bits when it is an int that is a 32 bit data-type.
This part of Java is not really on the OCAJP7 exam though?
Thank-you again for helping me understand what I was missing with the output of my program.
Regards,
Javanaut
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
I didn't say MAX_VALUE has only 31 bits. I said it has 31 1s. The MSB is 0 and is probably not printed because of the way toBinaryString method is coded.
No, this is not required for the exam.
HTH,
Paul.
No, this is not required for the exam.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Intense.
I had to look up MSB https://en.wikipedia.org/wiki/Most_significant_bit
I can see guess the MSB is dropped since it is a zero or something sort of like how octals are printed by Java if I am not mistaken.
Yes, I am glad this is not on the exam.
I had to look up MSB https://en.wikipedia.org/wiki/Most_significant_bit
I can see guess the MSB is dropped since it is a zero or something sort of like how octals are printed by Java if I am not mistaken.
Yes, I am glad this is not on the exam.
-
- Posts: 4
- Joined: Thu Jan 09, 2014 3:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Has this question ever appeared on the test?
I think most would choose the Overflow exception as the answer. This is totally unexpected behavior. There probably are many programs out there with bug related to this.
I think most would choose the Overflow exception as the answer. This is totally unexpected behavior. There probably are many programs out there with bug related to this.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
As mentioned in previous posts, this much detail about 1s and 2s complement is not required for the exam.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
I did not understand the explanation in programm, but i find this in the web:
is it correct?
Code: Select all
Basically, because Integer.MAX_VALUE is actually only 2147483647, so -Integer.MIN_VALUE, which would be +2147483648, actually overflows the capacity of the internal binary representation of integers. Thus, the result "loops around" back to Integer.MIN_VALUE, or -2147483648.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1159 :
Yes, the explanation explains the reason why "loop around" happens.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 112 guests