• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan Mock question

 
Sam Zou
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question about Dan Mock Study Guide exam.
Ths question is from the Study Guide Exam A , question 14,

In the answer it said:
....However, if it is cast to a byte then it is reduced to eight bitsthat are all set to one...

It is the reduced to eight bits that I don't understand. In general case, when there's a narrowing conversion, in our case to byte, do we always take the 8 less significant bits ?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt the opposite result is what anyone would expect. By narrowing the value you eliminate the portion of the original value that doesn't fit in the range of the narrower type.
There is a presumption that type narrowing isn't generally used to alter the value; usually you just want to eliminate zero-padding that occurs because the VM memory model wants to manage 32-bit values internally and leave the type management of "sub-integers" to the programmer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic