java 为什么我们需要在java中使用移位运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7454619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do we need to use shift operators in java?
提问by Saravanan
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?
使用 Shift 运算符而不是使用除法和乘法的目的是什么?
使用移位运算符还有其他好处吗?
应该在哪里尝试使用移位运算符?
采纳答案by Sean Owen
Division and multiplication are not reallya 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:
例如,假设我有两个字节,它们是两字节(16 位)无符号值的高位字节和低位字节。假设您需要构建该值。在 Java 中,这是:
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.
回答您的问题:您可以在需要使用它们的地方使用它们!没有其他地方。
回答by Bohemian
The shift operator is used when you're performing logicalbits operations, as opposed to mathematicaloperations.
与数学运算相反,在执行逻辑位运算时使用移位运算符。
It canbe 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.
它可用于提高速度,在处理 2 的幂的操作数时明显比除法/乘法快,但代码的清晰度通常优于原始速度。
回答by Muhammad Hewedy
It might also used in encryption/decryption .. Example: http://freedom2blog.com/2010/05/easy-encryption-using-bitwise-exclusive-or-xor/
它也可能用于加密/解密 .. 示例:http: //freedom2blog.com/2010/05/easy-encryption-using-bitwise-exclusive-or-xor/
回答by Charles Goodwin
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
"#AARRGGBB"
作为 base16 字符串0xAAAARRRRGGGGBBBB
作为整数
In its integer format, you can use shift to get the actual value of a component of the integer as a usable number.
在其整数格式中,您可以使用 shift 来获取整数组件的实际值作为可用数字。
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;
}
回答by Nikhil Kumar
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.
- 用算术移位或逻辑移位替换整数除法或乘法的 2 次方。
- 用移位、加法或减法的组合用常数代替整数乘法。
- 利用机器整数的有限范围,用乘法代替常数除法。
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
1.随着计算所需时间的增加而降低性能。2.除法和乘法等算术运算较慢。3. 昂贵的操作
Benefits
好处
- Improves performance.
- Faster calculations.
- 提高性能。
- 更快的计算。
Demerit
记过
- Readability of Code decreases.
- 代码的可读性降低。
回答by Alessandro
It is useful when you deal with flags, you could store in just one int
variable the information about active flags, see following please:
在处理标志时很有用,您可以将int
有关活动标志的信息存储在一个变量中,请参阅以下内容:
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).
如您所见,active_flags
数字为 2 和数字 8。我们仅将这些信息存储在一个变量中,其值为 10 (8+2)。