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.

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 Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3366

8

posted

0

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

public class test{ public static void main(String args[]){ byte x=3; x=(byte)~x; System.out.println(x); } } the ~ operator simply works like this. ~X is the same as (-X)-1: //just change the sign and deduct 1. //in this case ~x = (-3)-1 which is the same as -4.

sola yemi
Greenhorn

Joined: Sep 14, 2000
Posts: 9

posted

0

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.

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... ------------ Dev, Signed bit and 2's complement , that is the catch.

srival vas
Greenhorn

Joined: Oct 10, 2000
Posts: 10

posted

0

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.

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

psr krishna
Greenhorn

Joined: Oct 05, 2000
Posts: 12

posted

0

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

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