Forums Register Login

operators

+Pie Number of slices to send: Send
The answer of below question is 'g'.Can somebody please explain me how,as i am unable to figure out .

class EBH021 {
public static void main(String[] args) {
System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0
b. Prints: -1,4
c. Prints: 0x1f,8
d. Prints: 31,16
e. Run-time error
f. Compile-time error
g. None of the above

Thanks
+Pie Number of slices to send: Send
In the shift operator, 8 << -1 is the same as 8 << 31, so the 1 bit is shifted out of the int, so you have 0.

Also if you bitwise and -1 with another number, it doesn't change that number since the bit representation of -1 is all 1s.

0x1f = 31.

So none of the options match the output.
[ September 18, 2006: Message edited by: Keith Lynn ]
+Pie Number of slices to send: Send
 

Originally posted by Keith Lynn:
In the shift operator, 8 << -1 is the same as 8 << 31, so the 1 bit is shifted out of the int, so you have 0.



In the AND operator, (-1 & 0x1f) is the same as (0xffffffff & 0x1f), so the result is 0x1f... or when printed in decimal, 31.

Henry
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 544 times.
Similar Threads
Hi can anybody explain
Dan's Question: Operators and Assignments
shift operators
byte shift
tricky question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:33:58.