# lang fund Ques from dan mock exam

anushree ari

Ranch Hand

Posts: 98

posted 13 years ago

- 0

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

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

Posts: 14

posted 13 years ago

- 0

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

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

Posts: 257

Serdar Ozturk

Greenhorn

Posts: 14

posted 13 years ago

- 0

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

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

Posts: 1865

posted 13 years ago

- 0

The previous posts provide great explanations. Even so, I post the URL to a couple tutorials.

MAX_VALUE Tutorial

Base Conversion 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

Posts: 14

anushree ari

Ranch Hand

Posts: 98

anushree ari

Ranch Hand

Posts: 98

posted 13 years ago

- 0

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,

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

anushree ari

Ranch Hand

Posts: 98

anushree ari

Ranch Hand

Posts: 98

Dan Chisholm

Ranch Hand

Posts: 1865

posted 13 years ago

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.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>

- 0

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

Posts: 257

posted 13 years ago

- 0

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...

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...

Sunita<br />SCJP 1.4

Serdar Ozturk

Greenhorn

Posts: 14

posted 13 years ago

- 0

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

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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |