Results 1 to 7 of 7
  1. #1
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default where exactly bit shift operators are useful?

    Hi ti all,
    I am interested to know in which cases exactly << ,>> and >>> operators .I never used those operators in my programs or either i didn't any expressions working with those operators ,in some project examples which i had seen.
    Can u tell me in which case[in real world examples] they are used.I know what exactly they do, but i don't know where they should be useful.Please tell me some cases where they are useful.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I think the main use of these operators is a program that has to do a lot multiplication, division and modulus operations, sice bit shift operators execute in far less machine instructions than pure algebraic methods of calculation. I'm sure JosAh will be around soon, he knows a lot more about bitwise calculus than I do.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Colors are a great example; probably one of the better ones.

    Let's imagine you are given an RGB color value for gold: (255,128,0) with alpha 100% (also 255).
    Now, we are to convert this to a more bit-friendly form; hexadecimal. This means that using the values of ARGB put into hex format (and concatenated), it's readable to the computer--in this case, that would be 0xFFFF8000.
    Now, imagine that we don't know what the original ARGB values are. We only have 0xFFFF8000. And, our program wants to determine how much green is in that color (i.e. the 0x80 part).

    Firstly, we have to apply an & operator to get rid of everything BUT the green values. If you are familiar with the & operator, you will know that 0xFFFF8000 & 0xFF00 will result in 0x8000. If not, just ignore this step... it's not really too relevant to the shift itself, just to the example.

    Now, you have the number 0x8000 and want that to be 80. While we could divide this by 0x100, that's actually a bit silly, particularly if we're working with numbers we don't know. Instead, we shift it right 8 bits, so we end up with:
    Java Code:
    int color = 0xFFFF8000;
    color &= 0xFF00;
    color = color >> 0x8;
    // color == 0x80
    Hope that helps a little bit.

    It is worth noting that, although the tests are slightly inaccurate (because of Java's weirdness with stress tests), a bit operation is about 30 times faster than its equivalent using division/multiplication by 2.

  4. #4
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    I know only how to manipulate bitwise operators on bit values like 0 and 1 ,but i don't know how these can be used on hexadecimal values . Are they are more use full ?

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    They can be. I assume you know what hexadecimal is; imagine you have 0xF (1111 in binary), and you shift it left 8 bits. That's 111100000000 (1111 with 8 zeroes), which is also 0xF00. Notice a pattern? Try out some things like that if you want to get used to the commonality between hex and binary.

  6. #6
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    Actually ,I never worked on hexadecimal values ,thats why i am confused .From your explanation it looks very easy.I think i need to work some examples on my book first ,then it is easy to learn.

    Thank u.

  7. #7
    gazolinia is offline Member
    Join Date
    Aug 2010
    Location
    India
    Posts
    2
    Rep Power
    0

    Default

    In network programming when you play with generating TCP UDP packets, these bitwise operators will be very helpful in general .

Similar Threads

  1. Shift Off Trailing Zeroes
    By nwboy74 in forum New To Java
    Replies: 5
    Last Post: 02-25-2010, 07:56 AM
  2. Insert and Shift Array
    By topcat1325 in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 02:50 AM
  3. New to operators, need some help...
    By Stryker4526 in forum New To Java
    Replies: 4
    Last Post: 09-26-2009, 01:29 AM
  4. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 10:26 AM
  5. Run method on Ctrl+Shift+D
    By Echilon in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 10:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •