This week's book giveaway is in the Android forum. We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line! See this thread for details.

This below question is from the website -http://209.242.122.83/JavaCertification.html What is the result of the following fragment of code? (byte)0x81>>2 a. 0x20 b. 0x3FFFFFE0 c. 0XE0 d. 0xFFFFFFE0 I don't know the answer, but I want to know that, will this type of questions appear in the real exam Thank you.......

Hi Sdev, I'm a novice myself, but here is how I do the shifting. Refer RHE, It says If a binary number is shifted left one position, the effect of the shift is to double the original number. So the result is as if the number is multiplied by 2,4, 8,16 & so on. Similarly in shifting right , the result is as if the number is divided by 2, 4, 8 & so on..Please note, the case is different for negative numbers. If we take yr case, (byte)0x81>>2 1.Convert 0X81 to decimal. The equivalent decimal value is 129. 2.Shifting 129 >> 2, is equivalent to dividing 129 by 4. The value will be 32 (not 32.5) 3. Convert 32 to Hex. The value will be 0X20. So the answer will be the choice a. 0X20 Please feel free to correct me if I am wrong. -Hema

hello evreybody, the answer for this question will be 0XE0. explanation:--- 1) the given number ix in hex. 2)value for this in binary is 10000001. 3)after >>2, it will come out 11100000 bcoz during right shiftvacant bits are filled by 8th bit. i.e. if it is zero vacant bits will be filled by zero and if it is one vacants bits will be filled by one. 4)now 11100000 is to be changed in hex again and it is 0XE0. if i am wrong write me. gautam

I think Gautam is absolutely write the correct answer is 0XE0. If it was (byte)0x81 >>> 2 then the answer would be 0x20.

Sun Certified Java Programmer<BR>Oracle Certified DBA

sanjay gautam
Ranch Hand

Joined: Aug 13, 2000
Posts: 35

posted

0

hello sdev, ans of this question will be 0XE0. explanation---- 1)first convert this number in binary , it will come out 1000001 2)after >>2 it will be 11100000, bcoz of signed right shift. 3)again change it in hex it will come out 0xE0. i think i am right . if any doubt please write me , gautam

Actually byte is 8 bits. So, by looking at the operation a cast is preformed then the shift. If the upper most bit in the byte is 1 then the number is negative. Then the shift occurs. On a shift to the right the bits are shifted off and the sign bit is shifted in. Also, the return from a shift is int if not case. So, with all of that in mind the correct answer is D 0xFFFFFFE0

Originally posted by sdev: This below question is from the website -http://209.242.122.83/JavaCertification.html What is the result of the following fragment of code? (byte)0x81>>2 a. 0x20 b. 0x3FFFFFE0 c. 0XE0 d. 0xFFFFFFE0 I don't know the answer, but I want to know that, will this type of questions appear in the real exam Thank you.......

Originally posted by sanjay gautam: hello sdev, ans of this question will be 0XE0. explanation---- 1)first convert this number in binary , it will come out 1000001 2)after >>2 it will be 11100000, bcoz of signed right shift. 3)again change it in hex it will come out 0xE0. i think i am right . if any doubt please write me , gautam

Left operand of >> is always promoted to an int before the operation. Does the byte cast (byte) apply only to 0x81 or to the result after the operation. int x = 0x81; System.out.println((byte)x>>2) gives a result of -32. but, System.out.println((byte)(x>>2)) gives a result of 32. From the choices, it seems like the cast was for the result of the operation. So, the correct answer seems to be 32. Savithri

I have seen this question and it's answer as defined by the author. Additionally, I ran the code (when all eles fails...) Tim McCauley's explaination is correct. ...the correct answer is D 0xFFFFFFE0

This question is really tricky, I hope this kind of question is not going to appear in the real test. Who would want to shift a value of type byte in the first place? 1) cast has a higher precedence than shift operator, therefore 0x81 is cast to 10000001 (this is -1 in decimal). 2) however, the left operand of a shift operator should be of type int or long, therefore, the left operand is promoted to an integer before the shift and becomes 32 bit: 11111111 11111111 11111111 10000001 (so here -1 as a byte is promoted to a huge integer because the later is unsigned, irrational!! I don't know why we add 1 instead of 0?!), Then we do the signed shift of an unsigned integer,(again irrationally treating an unsigned number with a signed one!) and get 0xFFFFFFE0;

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

posted

0

I'm sorry about the obviously wrong statement that integer type is unsigned, I was thinking char type in my mind! sorry for the confusion, next time I'll be more careful! Chengx