Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift operators

 
Faqeer Khudaka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
Jan Valenta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks guys.
I didnt pay attention to the int promotion thing.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember this topic is on the 1.4 exam, but it's NOT on the 1.5 exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic