# hexadecimal format

apoorba mohapatra

Greenhorn

Posts: 7

posted 10 years ago

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

-----------

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

posted 10 years ago

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

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

posted 10 years ago

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 ]

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 ]

Not everything that counts can be counted, and not everything that can be counted counts.

Bert Bates

author

Sheriff

Sheriff

Posts: 8898

5

Enge Chall

Ranch Hand

Posts: 30

ghal prad

Greenhorn

Posts: 1

Enge Chall

Ranch Hand

Posts: 30