programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Paul Clapham
• paul wheaton
Sheriffs:
• Tim Cooke
• Devaka Cooray
• Rob Spoor
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:

# shiftoperators

Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
anyone please explain the following program
The following code will print
1: int i = 1;
2: i <<= 31;<br /> 3: i >>= 31;
4: i >>= 1;
5:
6: int j = 1;
7: j <<= 31;<br /> 8: j >>= 31;
9:
10: System.out.println("i = " +i );
11: System.out.println("j = " +j);
A) i = 1
j = 1
B) i = -1
j = 1

C) i = 1
j = -1

D) i = -1
j = -1

Ranch Hand
Posts: 1070
• Number of slices to send:
Optional 'thank-you' note:
chandana,
You name doesn't comply with our naming policy here at the Ranch. Take a look here for more info: www.javaranch.com/name.jsp
Please re-register with a more appropriate name.
Thanks
Bill

bill bozeman
Ranch Hand
Posts: 1070
• Number of slices to send:
Optional 'thank-you' note:
Take 1 and do a left shift 31 spaces and you get 100000000...
Now, do a signed right shift by 31 and you get 111111111....
Now do a signed right shift by 1 more and you get 11111111... still
Which leaves you with -1. Same for j but you don't do the last step, but since the last step doesn't do anything, it doesn't really matter.
Bill

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note: