jesse

Greenhorn

Posts: 3

posted 16 years ago

public class test{

public static void main(String args[]){

byte x=3;

x=(byte)~x;

System.out.println(x);

}

}

A. 3

B. 0

C. 1

D. 11

E. 252

F. 214

G. 124

H. -4

The correct answer was given as H. The answer i got was -124. Could someone please explain this question to me.

public static void main(String args[]){

byte x=3;

x=(byte)~x;

System.out.println(x);

}

}

A. 3

B. 0

C. 1

D. 11

E. 252

F. 214

G. 124

H. -4

The correct answer was given as H. The answer i got was -124. Could someone please explain this question to me.

Paul Anilprem

Enthuware Software Support

Ranch Hand

Ranch Hand

Posts: 3954

28

posted 16 years ago

I'm not sure how are you getting -124. Did you type it exactly as you've given here?

-Paul.

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

Get Certified, Guaranteed!

(Now Revised for the new Pattern)

www.enthuware.com/jqplus

-Paul.

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

Get Certified, Guaranteed!

(Now Revised for the new Pattern)

www.enthuware.com/jqplus

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications

Quality Guaranteed - Pass or Full Refund!

Paul Anilprem

Enthuware Software Support

Ranch Hand

Ranch Hand

Posts: 3954

28

posted 16 years ago

That's correct. ( but only if there is no overflow etc.) For eg. It won't work in cases like this:

int x = 128;

byte b = (byte) ~x; //here b will be 127! Now figure it out.

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

Get Certified, Guaranteed!

(Now Revised for the new Pattern)

www.enthuware.com/jqplus

int x = 128;

byte b = (byte) ~x; //here b will be 127! Now figure it out.

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

Get Certified, Guaranteed!

(Now Revised for the new Pattern)

www.enthuware.com/jqplus

Quality Guaranteed - Pass or Full Refund!

sola yemi

Greenhorn

Posts: 9

sola yemi

Greenhorn

Posts: 9

posted 16 years ago

Thanks for the info about the overflow, if i'd seen this in the exam, i would have been totally confused, thanks again.

Originally posted by Paul Anil:

That's correct. ( but only if there is no overflow etc.) For eg. It won't work in cases like this:

int x = 128;

byte b = (byte) ~x; //here b will be 127! Now figure it out.

Dev Prakash

Greenhorn

Posts: 28

posted 16 years ago

3 in binary(byte) is 00000011

if you inverse it, it should become 11111100 which is equal to 252 in decimal.

Is that not correct???

Please explain...

if you inverse it, it should become 11111100 which is equal to 252 in decimal.

Is that not correct???

Please explain...

srival vas

Greenhorn

Posts: 10

srival vas

Greenhorn

Posts: 10

posted 16 years ago

quote:

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

Originally posted by Paul Anil:

That's correct. ( but only if there is no overflow etc.) For eg. It won't work in cases like this:

int x = 128;

byte b = (byte) ~x; //here b will be 127! Now figure it out.

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

just to clarify though I am sure you all realised it,

b is 127 not bcos of ~, but bcos -129 (-128 -1 ) has

been casted to byte, I would assume I can still safely

assume ~n = -n -1 and then concern myself with the casts et al.

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

Originally posted by Paul Anil:

That's correct. ( but only if there is no overflow etc.) For eg. It won't work in cases like this:

int x = 128;

byte b = (byte) ~x; //here b will be 127! Now figure it out.

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

just to clarify though I am sure you all realised it,

b is 127 not bcos of ~, but bcos -129 (-128 -1 ) has

been casted to byte, I would assume I can still safely

assume ~n = -n -1 and then concern myself with the casts et al.

psr krishna

Greenhorn

Posts: 12

posted 16 years ago

hi jesse,

The answer for your question is -4. how?

here is explanation....

public class test{

public static void main(String args[]){

byte x=3;

x=(byte)~x;

System.out.println(x);

}

}

A. 3

B. 0

C. 1

D. 11

E. 252

F. 214

G. 124

H. -4

byte x=3 is in 8 bit representation so, in binary it is 00000011 OK! then

~x= bit wise inversion is 11111100 OK! after this inversion

when right most bit is 1 it will be considered as a

negative value and it is assigned to x.In JVM the negative numbers will be represented in 2's complement form ,so to get actual number you first again inverse it and add 1 to last significant bit and express your result in nagative value, so this is like this

x: 00000011

~x: 11111100

for result inverse the above ~(~x): 00000011

add 1 to left most bit 1

--------

00000100 which is 4 in

--------

decimal notation and express result in negative i.e.:-4

OK! NOW I THINK YOUR DOUBT IS CLEARED.

---THANKS

SHIVARAM

The answer for your question is -4. how?

here is explanation....

public class test{

public static void main(String args[]){

byte x=3;

x=(byte)~x;

System.out.println(x);

}

}

A. 3

B. 0

C. 1

D. 11

E. 252

F. 214

G. 124

H. -4

byte x=3 is in 8 bit representation so, in binary it is 00000011 OK! then

~x= bit wise inversion is 11111100 OK! after this inversion

when right most bit is 1 it will be considered as a

negative value and it is assigned to x.In JVM the negative numbers will be represented in 2's complement form ,so to get actual number you first again inverse it and add 1 to last significant bit and express your result in nagative value, so this is like this

x: 00000011

~x: 11111100

for result inverse the above ~(~x): 00000011

add 1 to left most bit 1

--------

00000100 which is 4 in

--------

decimal notation and express result in negative i.e.:-4

OK! NOW I THINK YOUR DOUBT IS CLEARED.

---THANKS

SHIVARAM

psr krishna

Greenhorn

Posts: 12

posted 16 years ago

hi jesse,

please correct a little mistake in my previous post

byte x=3 is in 8 bit representation so, in binary it is 00000011 OK! then

~x= bit wise inversion is 11111100 OK! after this inversion

when left most bit is 1 it will be considered as a

negative value and it is assigned to x.In JVM the negative numbers will be represented in 2's complement form ,so to get actual number you first again inverse it and add 1 to right significant bit and express your result in nagative value, so this is like this

x: 00000011

~x: 11111100

for result inverse the above ~(~x): 00000011

add 1 to right most bit 1

--------

00000100 which is 4 in

--------

decimal notation and express result in negative i.e.:-4

OK! NOW I THINK YOUR DOUBT IS CLEARED.

---THANKS

SHIVARAM

please correct a little mistake in my previous post

byte x=3 is in 8 bit representation so, in binary it is 00000011 OK! then

~x= bit wise inversion is 11111100 OK! after this inversion

when left most bit is 1 it will be considered as a

negative value and it is assigned to x.In JVM the negative numbers will be represented in 2's complement form ,so to get actual number you first again inverse it and add 1 to right significant bit and express your result in nagative value, so this is like this

x: 00000011

~x: 11111100

for result inverse the above ~(~x): 00000011

add 1 to right most bit 1

--------

00000100 which is 4 in

--------

decimal notation and express result in negative i.e.:-4

OK! NOW I THINK YOUR DOUBT IS CLEARED.

---THANKS

SHIVARAM

Raghvendra Sharma

Ranch Hand

Posts: 82

posted 16 years ago

The answer is -4 only.

Try it with the jdk and see the result yourself.

3 decimal = 0000 0011 ( binary )

when you invert it becomes 1111 1100 ( binary )

That's 2complement representation of -4 ( check by 1. inverting and 2. adding 1 to result of inversion ).

You should check once more.

i hope it helps.

raghav..

Try it with the jdk and see the result yourself.

3 decimal = 0000 0011 ( binary )

when you invert it becomes 1111 1100 ( binary )

That's 2complement representation of -4 ( check by 1. inverting and 2. adding 1 to result of inversion ).

You should check once more.

i hope it helps.

raghav..

Consider Paul's rocket mass heater. |