feng lee

Ranch Hand

Posts: 33

posted 14 years ago

public static void main(String args[])

{

int t=0xffff1;

int j=~t;

System.out.println(j);

}

What will be the output?

A. -1048562

B. -10

C. Exception

D. Compilation Error

Ans:A

why?

{

int t=0xffff1;

int j=~t;

System.out.println(j);

}

What will be the output?

A. -1048562

B. -10

C. Exception

D. Compilation Error

Ans:A

why?

david eberhardt

Ranch Hand

Posts: 158

posted 14 years ago

steps:

1) convert Oxffff1 to a 32 bit binary number

working backwards from above

1 = 0001

f = 1111

f = 1111

f = 1111

f = 1111

is 1111 1111 1111 1111 0001

that's a total of 20 bits so far.

since this is a positive number, we will add enough zeroes to total 32 bits:

0000 0000 0000

------------------------------------------------

2) now we apply the

1111 1111 1111 0000 0000 0000 0000 1110

so now this is stored in the computer.

-------------------------------------------------

3)

4) now we will attempt to print out this number, so two's compliment conversion is applied:

a) we apply the ~ operator again:

0000 0000 0000

b) add 1 to above and get

0000 0000 0000

now we convert this binary to decimal:

the leftmost 1 above is going to yield 2^19 = 524,288

the next 1 gives us 2^18 = 262,144

and so on ....

remember that when we get to the last 4 binary digits (0010) we have added up all these numbers as we go, for a total of 1048560 to this point:

the last four positions yield

2^3 *0 = 0 // 2^3 = 8

2^2 *0 = 0 // 2^2 = 4

2^1 *1 = 2 // 2^1 = 2

2^0 *0 = 0 // 2^0 = 1

So 1048560 + 2 = 10485562

And finally we put the negative sign in front to get -10485562 when we println(j)

steps:

1) convert Oxffff1 to a 32 bit binary number

working backwards from above

1 = 0001

f = 1111

*(which is 15 in decimal)*

f = 1111

f = 1111

f = 1111

is 1111 1111 1111 1111 0001

that's a total of 20 bits so far.

since this is a positive number, we will add enough zeroes to total 32 bits:

0000 0000 0000

**1111 1111 1111 1111 0001**

------------------------------------------------

2) now we apply the

**~**operator which flips each bit to it's opposite:

1111 1111 1111 0000 0000 0000 0000 1110

so now this is stored in the computer.

-------------------------------------------------

3)

**when we print this number it will be a negative number displayed**because the highest bit

*(to the left above)*is a 1

4) now we will attempt to print out this number, so two's compliment conversion is applied:

a) we apply the ~ operator again:

0000 0000 0000

**1111 1111 1111 1111 0001**

b) add 1 to above and get

0000 0000 0000

**1111 1111 1111 1111 0010**

now we convert this binary to decimal:

the leftmost 1 above is going to yield 2^19 = 524,288

the next 1 gives us 2^18 = 262,144

and so on ....

remember that when we get to the last 4 binary digits (0010) we have added up all these numbers as we go, for a total of 1048560 to this point:

the last four positions yield

2^3 *0 = 0 // 2^3 = 8

2^2 *0 = 0 // 2^2 = 4

2^1 *1 = 2 // 2^1 = 2

2^0 *0 = 0 // 2^0 = 1

So 1048560 + 2 = 10485562

And finally we put the negative sign in front to get -10485562 when we println(j)

*note: int t was 1048561*

and int j is -1048562

(the number 0 counts as a position on the number line)

and int j is -1048562

(the number 0 counts as a position on the number line)

david eberhardt

Ranch Hand

Posts: 158

posted 14 years ago

oh, I know that was alot of work ... but I wanted to show evey detail.

Once we converted the hex number 0xffff1 to binary, we knew we had a big number because the leftmost 1 was 2^19 = 524,288 plus all the others...

then we were going to ~ that number turning it to some big negative number so that eliminated the answer -10.

next, we could eliminate the exception answer because we would not exceed the max value for an int at runtime!

compilation error is elimated because we didn't have to worry about casting and other things ... the code was written properly in all other aspects.

we just have to remember the +1 part when doing the two's compliment routine when we bring a negative number out to print it.

Hope this helps!

Once we converted the hex number 0xffff1 to binary, we knew we had a big number because the leftmost 1 was 2^19 = 524,288 plus all the others...

then we were going to ~ that number turning it to some big negative number so that eliminated the answer -10.

next, we could eliminate the exception answer because we would not exceed the max value for an int at runtime!

compilation error is elimated because we didn't have to worry about casting and other things ... the code was written properly in all other aspects.

we just have to remember the +1 part when doing the two's compliment routine when we bring a negative number out to print it.

Hope this helps!

feng lee

Ranch Hand

Posts: 33

feng lee

Ranch Hand

Posts: 33