• 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

Bit Shifting

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I understand that shift operator operands are promoted to an int before the shift is run. But it is not clear what is happening on line (1). Why do I not need to cast here (1) when I need to do an explicit cast on line (3).



Thoughts appreciated.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arthur Blair:
Ok, I understand that shift operator operands are promoted to an int before the shift is run. But it is not clear what is happening on line (1). Why do I not need to cast here (1) when I need to do an explicit cast on line (3).



Thoughts appreciated.


that is the way shortcut assignment operators work
accutally
b<<=2 is b=(cast)b<<2
refer K&B chapter 3

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compound assngment does the casting implicitly.

so when u say,

byte b = 7
b+=2;

will also work fine.

what it really does is-->
b= (byte)(b+2);

that is
1. promote b to int.
2. add 2 to it.
3. cast the result which is 9 (b+2)to byte.
4. and assign it back to b.

i hope that cleared your doubts.

Thanks
-VK
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic