aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes lang fund Ques from dan mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "lang fund Ques from dan mock exam" Watch "lang fund Ques from dan mock exam" New topic
Author

lang fund Ques from dan mock exam

anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi guys,
i came across this question from dan's topic exam,how to convert integer min value to hex,is there any short cut?
class B {
public static void main(String args[]) {
System.out.print(Integer.toHexString(Integer.MIN_VALUE)+",");
System.out.print(Integer.toHexString(Integer.MAX_VALUE));
}
}
expecting the result soon,
thx


anushree
Serdar Ozturk
Greenhorn

Joined: Aug 12, 2002
Posts: 14
I dont know is there a short way of doing it, but this is the way I do generally.
Write the number in binary format, write each 4 binary digit as a separate group.
MIN : 1000 0000 0000 0000 0000 0000 0000 0000
Now count decimal values for each group
8 0 0 0 0 0 0 0
Convert decimals to HEX
80000000
MAX : 0111 1111 1111 1111 1111 1111 1111 1111
7 15 15 15 15 15 15 15
7FFFFFFF
Serdar
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Does this technique work for the other integer types also like short, byte...
I tried running with short and it gives minimum hex value as ffff 8000. Why is it so? If we run with byte it gives result as 7f and ffffff80...


Sunita<br />SCJP 1.4
Serdar Ozturk
Greenhorn

Joined: Aug 12, 2002
Posts: 14
When you write System.out.print(Integer.toHexString(Short.MIN_VALUE), short value is first converted to int
Short.MIN = -2^15
and it's 1000 0000 0000 0000 = 0x7000
, be careful that when you convert this negative short value to int it's gonna be a different bit sequence
2's compliment calculation of -2^15
+2^15 = 0000 0000 0000 0000 1000 0000 0000 0000
take inverse of each bit
1111 1111 1111 1111 0111 1111 1111 1111
Add 1
1111 1111 1111 1111 1000 0000 0000 0000
0xFFFF8000

For maximum values there is no problem
Short.MAX_VALUE = 0111 1111 1111 1111
= 0x7FFF
when you convert it to int
0000 0000 0000 0000 0111 1111 1111 1111
0x00007FFFF
but the methos toHexString does not print the first four zeros, so the result is 7FFF
Serdar
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The previous posts provide great explanations. Even so, I post the URL to a couple tutorials.
MAX_VALUE Tutorial
Base Conversion Tutorials


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Serdar Ozturk
Greenhorn

Joined: Aug 12, 2002
Posts: 14
Sorry, I've made a small mistake there,
Short.MIN = -2^15
and it's 1000 0000 0000 0000 = 0x7000
should be corrected as = 0x8000
Serdar
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
thx guys,
dan your tutorial is helping me a lot, thx, serdar.
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi serdar,
i compiled the min value of byte, i got these results,
System.out.print(Integer.toHexString(Byte.MIN_VALUE))
the result is ffff ff80
in binary
System.out.print(Integer.toBinaryString(Byte.MIN_VALUE))

1111111111111111111111111000000;
but your's different,
pls anybody correct me,
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
dan,
when i studied your material, you explained the max value, could u pls explain min value with some example,
it would be easier for me,
thx,i'll expect the results,
bye
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi sunitha,
the max value of byte is 127,
when u r converting to hex that means divide the value by 16, u get 7 remainder 15, the value of 15 in hex is f, so the result is 7f, got it.
bye
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by anushree ari:
dan,
when i studied your material, you explained the max value, could u pls explain min value with some example,
it would be easier for me,
thx,i'll expect the results,
bye

Before I write a tutorial on MIN_VALUE I will have to first write a tutorial on two's compliment arithmetic. I'm certain that I won't have time to do it today, but maybe sometime next week.
Thank you for using my exam.
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Consider Byte data type for example.. The min value is -128....
All negative numbers are stored in 2's complement notation...
It is calculated as follows...
128 = 2^7... so put 7 0's after 1...
You get
10000000
When u store it in 2's complement notation you get the representation of -128...
1) First take 1's complement.. Change all 1's to 0's and 0's to 1's..
2) Add 1
Since it is an Integer.toHexString it has to take a 32 bit value whereas a byte has only 8 bits...
So it takes something like 00000000 00000000 00000000 1000 0000 to start with and when it finds the 2's complement as described above :-
00000000 00000000 00000000 10000000
Taking 1's complement
11111111 11111111 11111111 01111111
Then adding 1 we get
11111111 11111111 11111111 10000000
which would give
ffffff80...
Hope this helps...
Serdar Ozturk
Greenhorn

Joined: Aug 12, 2002
Posts: 14
Well, I think it should be clear for everyone now,but the following may be another short-hand way of thinking.
Byte.MIN = 80
Byte.MAX = 7F
Short.MIN = 8000
Short.MAX = 7FFF
While converting those numbers (in fact, we can use this rule for any number )to a wider integral type(int or long),
for negative numbers: Add F's to the left
for positive numbers: Add 0's to the left
So,
byte -128: 80 --> FFFFFF80
short -32768: 8000 --> FFFF8000
7F --> 0000007F
7FFFF --> 00007FFF

PS: For negative numbers, remember that >> operator inserts "1" into the leftmost digit to keep the number negative and to allow division and multiplication by 2.
Serdar
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
thx sunita,serdar,
i'll practice very well with the binary, now iam having clear idea about -ve and +ve values,
thx guys,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lang fund Ques from dan mock exam