Results 1 to 7 of 7
 09072010, 01:19 PM #1Member
 Join Date
 Feb 2009
 Posts
 96
 Rep Power
 0
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.
 09082010, 12:08 AM #2Senior Member
 Join Date
 Feb 2010
 Location
 Ljubljana, Slovenia
 Posts
 470
 Rep Power
 11
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.
 09082010, 12:10 AM #3
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 bitfriendly form; hexadecimal. This means that using the values of ARGB put into hex format (and concatenated), it's readable to the computerin 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
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.
 09082010, 05:14 AM #4Member
 Join Date
 Feb 2009
 Posts
 96
 Rep Power
 0
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 ?
 09082010, 07:11 AM #5
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.
 09082010, 10:01 AM #6Member
 Join Date
 Feb 2009
 Posts
 96
 Rep Power
 0
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.
 09082010, 11:58 AM #7Member
 Join Date
 Aug 2010
 Location
 India
 Posts
 2
 Rep Power
 0
Similar Threads

Shift Off Trailing Zeroes
By nwboy74 in forum New To JavaReplies: 5Last Post: 02252010, 07:56 AM 
Insert and Shift Array
By topcat1325 in forum New To JavaReplies: 4Last Post: 02052010, 02:50 AM 
New to operators, need some help...
By Stryker4526 in forum New To JavaReplies: 4Last Post: 09262009, 01:29 AM 
Anyway to fix the lines so they dun shift?
By PeterFeng in forum New To JavaReplies: 0Last Post: 01142009, 10:26 AM 
Run method on Ctrl+Shift+D
By Echilon in forum AWT / SwingReplies: 1Last Post: 02162008, 10:31 PM
Bookmarks