• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shift operators

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


I found this question in one of the mock exams:



Correct answer is A & B. My answer was B&C cause when 128 >> 1 is computed, the rule is that whatever the most significant bit is, that has to be "filled" from the left. In this case when 128(10000000) is shifted right by 1 bit, another 1 will be added cause initially the most significant bit of 128 was 1. This will give us 11000000 which comes out to be 192.
Am i correct??

Thanks
Faqeer
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget that there is automatic promotion of the operands to ints. Therefore you are shifting (00000000 00000000 00000000 10000000) and the initial zero stays zero.

Jan
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the correct answer is 64 for both



http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html

considering 128 as int datatype

128 value is 00000000 00000000 00000000 10000000
based on the referred link above

>> 00000000 00000000 00000000 01000000 (fills with highest (sign) bit on the left side )
>>> 00000000 00000000 00000000 01000000 (fills with 0 bits on the left side )
 
Faqeer Khudaka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.
I didnt pay attention to the int promotion thing.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember this topic is on the 1.4 exam, but it's NOT on the 1.5 exam.
 
reply
    Bookmark Topic Watch Topic
  • New Topic