Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hexadecimal format

 
apoorba mohapatra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 15
-----------
class JJF5 {
public static void main(String args[]) {
System.out.print(Integer.toHexString(Integer.MIN_VALUE)+",");
System.out.print(Integer.toHexString(Integer.MAX_VALUE));
}}


from [http://www.danchisholm.net/dec20/guide/chapter2/exam1.html]

How should I calculate the output?
Int has min value -2^ 31.
So do we need to convert that value to hexadecimal form and get the output?
But calculating this stuff would be very lengthy.

BTW the answer is: Prints: 80000000,7fffffff
 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.print(Integer.toHexString(Integer.MIN_VALUE)+",");
System.out.print(Integer.toHexString(Integer.MAX_VALUE));

Int is 32bits (with 1 sign bit)
so Min value of int(which is -ve) in its hex representation is
for -ve sign bit is 1
for +ve sign bit is 0

Sign Bit + 31 zeros
1 000 0000 0000 0000 0000 0000 0000 0000 [min value of int]
so output is: 80000000

Max Value is +ve
Sign Bit + 31 1's
0 111 1111 1111 1111 1111 1111 1111 1111
so output is: 7fffffff
 
Enge Chall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Apoorba, further to make it simple to face simillar questions you must be familliar with one's complement and two's complement in your studies.
It goes like this:
'-' ve numbers are represented in 2's complement form in computer memory.
What is 2's complement form ? Ans: It's one's complement + 1 ie add 1 to one's complement.
What is one's complement ? Ans: Just alter the digits of your binary numbers. ( if 1 make it 0, if 0 make it 1 ).

How -1 is represented ? Ans: It's 1 with a -ve sign ie two's complement of +1 with a -ve sign.
What is -ve sign and +ve sign ? Ans: The MSB is switched on ( ie MSB is 1 for -ve ), MSB is switched off( ie MSB is 0 for +ve ).

What is MSB ? Ans: The left most bit called Most Significant Bit .

==> let's eg: find out how -1 is stored in computer <==
So keeping that in mind now find out the 2's complement of 1. As java int is 4 bytes ie 32 bits, the 32nd bit is the MSB.

Now let's calculate:
+1 is 0000 0000 0000 0000 0000 0000 0000 0001
-1 is 2's complement of +1 ie 1's complement of +1 added by 1 as below :
1111 1111 1111 1111 1111 1111 1111 1110 +
0000 0000 0000 0000 0000 0000 0000 0001
---------------------------------------
1111 1111 1111 1111 1111 1111 1111 1111 ( See here MSB is switched on automatically for -ve )

Now coming to your question ie -2^31 is the minimum -ve value of int in java ( I am sure , you know how ? )

Now let's calculate:
+2^31 is 1000 0000 0000 0000 0000 0000 0000 0000
-2^31 is 2's complement of +2^31 ie 1's complement of +2^31 added by 1 as below :
0111 1111 1111 1111 1111 1111 1111 1111 +
0000 0000 0000 0000 0000 0000 0000 0001
---------------------------------------
1000 0000 0000 0000 0000 0000 0000 0000 ( See here MSB is switched on automatically for -ve )

Now convert to hexa : 80000000 ( every 4 bits together )

Simillarly with +ve numbers MSB is 0 ( switched off ).
Max +ve number is => (2^32)-1 ie
0111 1111 1111 1111 1111 1111 1111 1111 ( No calculation requires here as it is stored as it is )
So hex of this is : 7FFFFFFF

PLEASE LET ME KNOW IF YOU NEED SHORTCUT TO THIS PROCEDURE TO CALCULATE FASTER.

Wish you GOOD LUCK for your SCJP...
[ December 07, 2005: Message edited by: Enge Chall ]
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys -

Just to clarify, this topic is on the 1.4 exam, but it's NOT on the Tiger exam...

carry on
 
Enge Chall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The algorithm looks tiger because of 32 bits. The same could be imagined for 4 bits for simplicity. cheers

In case someone gets any other -ve number, they can immediately pick the answer.
 
ghal prad
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need that 4 bits procedure for simplicity
I have not understood this.

Can some ome help please?
 
Enge Chall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly you need in the 4 bit ?

Just take off all the 1's and 0's keeping the MSB and right side 3 bits as it is. That cut-shorts the procedure.

does that answer ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic