# where exactly bit shift operators are useful?

• 09-07-2010, 01:19 PM
sandeepsai39
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.
• 09-08-2010, 12:08 AM
m00nchile
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.
• 09-08-2010, 12:10 AM
Zack
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:
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.
• 09-08-2010, 05:14 AM
sandeepsai39
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 ?
• 09-08-2010, 07:11 AM
Zack
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.
• 09-08-2010, 10:01 AM
sandeepsai39
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.
• 09-08-2010, 11:58 AM
gazolinia
In network programming when you play with generating TCP UDP packets, these bitwise operators will be very helpful in general .