What is the purpose of using Shift operators rather than using division and multiplication?
Are there any other benefits of using shift operators?
Where should one try to use the shift operator?
-
2[Practical applications of bit shifting](https://stackoverflow.com/q/9455941/995714), [practical applications of bitwise operations](https://stackoverflow.com/q/3883384/995714), [Real world use cases of bitwise operators](https://stackoverflow.com/q/2096916/995714), [Have you ever had to use bit shifting in real projects?](https://stackoverflow.com/q/520625/995714) – phuclv Feb 01 '19 at 02:27
6 Answers
Division and multiplication are not really a use of bit-shift operators. They're an outdated 'optimization' some like to apply.
They are bit operations, and completely necessary when working at the level of bits within an integer value.
For example, say I have two bytes that are the high-order and low-order bytes of a two-byte (16-bit) unsigned value. Say you need to construct that value. In Java, that's:
int high = ...;
int low = ...;
int twoByteValue = (high << 8) | low;
You couldn't otherwise do this without a shift operator.
To answer your questions: you use them where you need to use them! and nowhere else.

- 66,182
- 23
- 141
- 173
-
i heard,it makes faster integer division/multiplication operations than *,/ – Saravanan Sep 17 '11 at 12:22
-
3Shifting left by 1 is faster than multiplying by 2. But, your JIT compiler and your processor know this better than you, and should do that automatically. In any event, this is not the *primary* use of shifts; it's arguably not even a good use. – Sean Owen Sep 17 '11 at 12:25
-
11not in java. Not even in C these days. The compilers are smart enough to optimize your code. It's best to make sure your code is readable and expresses what it wants to do rather than trying to outsmart the compiler and make it unreadable. – Savvas Dalkitsis Sep 17 '11 at 12:25
-
1shift operators aren't _necessary_, are just _convenient_, due to the fact a left (right) shift by n is equivalent to a multiplication (division) by 2^n. – akappa Sep 17 '11 at 12:25
-
1@akappa actually that's a fairly good answer -- except that you can't achieve an unsigned shift ('>>>' in Java) with division. – Sean Owen Sep 17 '11 at 13:53
The shift operator is used when you're performing logical bits operations, as opposed to mathematical operations.
It can be used for speed, being significantly faster than division/multiplication when dealing with operands that are powers of two, but clarity of code is usually preferred over raw speed.

- 412,405
- 93
- 575
- 722
It is useful in constructing values which are a combination of numbers, where bits are grouped as different values themselves. (Sean Owen's answer explains this better.)
For example, working with colours which are:
"#AARRGGBB"
as a base16 string0xAAAARRRRGGGGBBBB
as an integer
In its integer format, you can use shift to get the actual value of a component of the integer as a usable number.
public static int stringToColor(String s) throws JSExn {
// string starts with '#' - parse integer from string
try {
// used to build up the return value
int a, r, g, b;
switch (s.length()) {
case 4:
a = 0xFF000000;
r = Integer.parseInt(s.substring(1, 2), 16);
r = r << 16 | r << 20;
b = Integer.parseInt(s.substring(2, 3), 16);
b = b << 8 | b << 12;
g = Integer.parseInt(s.substring(3, 4), 16);
g = g | g << 4;
break;
case 5:
a = Integer.parseInt(s.substring(1, 2), 16);
a = a << 24 | a << 28;
r = Integer.parseInt(s.substring(2, 3), 16);
r = r << 16 | r << 20;
b = Integer.parseInt(s.substring(3, 4), 16);
b = b << 8 | b << 12;
g = Integer.parseInt(s.substring(4, 5), 16);
g = g | g << 4;
break;
case 7:
a = 0xFF000000;
r = Integer.parseInt(s.substring(1, 3), 16) << 16;
b = Integer.parseInt(s.substring(3, 5), 16) << 8;
g = Integer.parseInt(s.substring(5, 7), 16);
break;
case 9:
a = Integer.parseInt(s.substring(1, 3), 16) << 24;
r = Integer.parseInt(s.substring(3, 5), 16) << 16;
b = Integer.parseInt(s.substring(5, 7), 16) << 8;
g = Integer.parseInt(s.substring(7, 9), 16);
break;
default:
throw new JSExn("Not a valid color: '"+s+"'");
}
// return our integer ARGB
return a | r | b | g;
}

- 6,402
- 3
- 34
- 63
Strength reduction occurs when an operation is replaced by an equivalent operation that executes faster.
- replacing integer division or multiplication by a power of 2 with an arithmetic shift or logical shift..
- replacing integer multiplication by a constant with a combination of shifts, adds or subtracts.
- replacing integer division by a constant with a multiplication, taking advantage of the limited range of machine integers.
Why is this wrong?
1.Reduces performance as time required for calculation increases. 2. Arithmetic Operations like division and multiplication are slower. 3. Expensive operations
Benefits
- Improves performance.
- Faster calculations.
Demerit
- Readability of Code decreases.

- 2,618
- 3
- 21
- 24
It is useful when you deal with flags, you could store in just one int
variable the information about active flags, see following please:
public class DealingWithShiftOperators {
public static void main(String[] args) {
int active_flags = 10;
printActiveFlags(active_flags);
}
public static void printActiveFlags(int active_flags) {
final int TOTAL_FLAGS = 8;
final int MAX_VALUE = 1 << TOTAL_FLAGS;
final int MIN_VALUE = 1;
int current_flag = MAX_VALUE;
do {
current_flag = current_flag >> 1;
if (active_flags - current_flag < 0) {
System.out.println(current_flag + ": off");
} else {
active_flags = active_flags - current_flag;
System.out.println(current_flag + ": on");
}
} while (current_flag > MIN_VALUE);
}
}
The above example prints the follow to the output:
128: off
64: off
32: off
16: off
8: on
4: off
2: on
1: off
As you can see, the active_flags
are number 2 and number 8. We stored that information in just one variable, its value is 10 (8+2).

- 4,382
- 8
- 36
- 70
It might also used in encryption/decryption .. Example: http://freedom2blog.com/2010/05/easy-encryption-using-bitwise-exclusive-or-xor/

- 29,102
- 44
- 127
- 219
-
1The link is not working. But I found another page that explains same concept. http://crypto.stackexchange.com/questions/19470/how-is-xor-used-for-encryption – shreeneewas Mar 27 '17 at 09:05