• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

operators

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic