• 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

Re: Shift Operators

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain how shifting is done(right, left, sighned and unsighned) of positive and negative numbers.
Also are there any good links on the web regarding the same.
Thanks
Vineet
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look on Marcus Green's site for a tutorial and applet demonstrating shift operators
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone give me a real world example of when you would need to do bitshifting? I have never had to deal with binary manipulation in my programming experience and It's sort of a gray area to me.
Thanks.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,
I can give you two examples.
1. Given an int value (e.g. 634), can you tell many how bits are in this number? With bit shifting, you can get the answer with 4 lines of code. Can you figure it out? See if you can get the answer. If not, come back and I'll show you the code.
2. This is one we do a lot. If you're scarce on memory (e.g. if you were writing code for an embedded device such as a cellular phone), you would want to utilize the bit fields to hold as much information as possible. Especially for if they're flag fields. For example, if I had flags to represent a stop light signal (green, yellow, red) there are several ways I can store this information in code. Listed are most expensive to least expensive.
* Using String objects ("red", "yellow", "red")
* Using final private static int
* Use the bit position in a byte (e.g. bit 0 is red, bit 1 is yellow, and bit 2 is red).
-Peter
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Peter:
I don't quite understand your first question. Since every int is 32 bits, I think what you mean is to show the binary form of 634. I usually do it manually (it's not that hard actually), but I'm interested to know you can get the result with bit-shifting. Could you post the code? Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic