Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
使用移位运算符时,请务必小心,不要重复常见错误!
正如以下 SO post 所示,已接受答案的作者提到:
在对字节进行操作时,记住这一点绝对至关重要,否则您可能会得到意想不到的结果(就像我所做的那样)。
给定一个具有以下位模式的字节:
当我尝试按 4 位移位时,并分配给一个 int,例如:
我希望有:
但我会得到一个巨大的数字! 那是因为 byteValue 在位移操作之前被转换为 int ,从而导致这样的结果 反而:
When using the shift operator, be very careful not to repeat a common error!!
As the following SO post suggests, the author of the accepted answer mentions:
This is absolutely crucial to remember when operating on bytes for example, otherwise you may get unexpected results (as I did).
Given a byte with the following bit pattern:
When I tried to bit shift by 4, and assigned to an int, such as:
I would expect to have:
But I would get a HUGE number! That's because the byteValue is casted to int BEFORE the bit shift operation, thus resulting in something like this instead:
有无数种可能的组合。然而,它们将由一种或多种组合组成。
为了获得理解,我建议您将二进制数写在纸上并计算出会发生什么。尝试在教程中阅读并不能保证理解。尤其是如果他们到目前为止还没有提供帮助的话。
There is an infinite number of possible combinations. However they will be made up of one or more combinations of
To get an understanding I suggest you write the binary numbers on paper and work out what happens. Trying to read it in a tutorial won't guarantee understanding. esp if they haven't helped so far.
我发现这里有一个简单但清晰的教程很有用这里
There is simple but clear tutorial that I find useful here
这不完全是一个教程,但我有一个个人Java 中的位移函数库,非常欢迎您学习!
另外,如果您在 google 上搜索“位运算技巧”,您会发现很多材料。其中许多都是用 C/C++ 编写的,但通常很容易转换为 Java,因为大多数语法是相同的。
It's not exactly a tutorial, but I have a personal library of bit-shifting functions in Java which you are very welcome to study!
Also if you do a google search for "bitwise tricks" you will find a lot of material. Many of these are in C/C++ but are generally trivially to convert to Java as most of the syntax is the same.
以下是位移位工作原理的详细信息。
有一些官方教程未涵盖的非直观行为。例如,右操作数的范围有限(int 为 0-31,long 为 0-63),如果超出该范围,不会产生警告 - 它只会截断位(即 %32 或 %64) ),这可能会产生与您预期不同的行为。
Here are the details of how bit shifting works.
There is some non-intuitive behavior that is not covered by the official tutorial. For instance, the right operand has a limited range (0-31 for int, 0-63 for long), and will not produce a warning if you exceed that range -- it will just truncate the bits (i.e. %32 or %64), which may give behavior other than you expect.
这个网站似乎提供了一个很好的教程,介绍了如何使用位操作(所以不是特定于 java,但因为它很容易翻译)
http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.html
上面的教程提供了
这是一个包含一堆 java 实现的文件
http://geekviewpoint.com/
This site seems to give a pretty good tutorial on what you can do with bit manipulation (so not specific to java but since it is pretty easy to translate)
http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.html
The tutorial above provides
Here's a file that has a bunch of java implementations
http://geekviewpoint.com/
这是我在学习位移位时发现的两个很好的教程,它们不是 Java 中的,但大多数语言使用相同的运算符,并且理论是相同的。
These are two good tutorials i found while learning about bit shifting, they arent in java but most languages use the same operators and the theory is the same.
好吧,官方 Java 教程按位和位移运算符涵盖了Java 中可用的实际操作以及如何调用它们。
如果您想知道“我可以用位移位做什么”,那么这不是 Java 特有的,并且由于它是一种低级技术,我不知道任何“您可以做的很酷的事情”本身的列表。熟悉这些定义,并密切关注使用此定义的其他代码,看看它们做了什么,这是值得的。
请注意,经常进行位调整是为了提高效率,但牺牲了清晰度。例如,
a << 1
通常与a * 2
相同,但可能不太清楚。重复的 XOR 可以在不使用临时变量的情况下交换两个数字,但通常认为使用临时变量更清楚地编写代码是更好的形式(或者在实用程序方法中更好)。因此,在这方面很难给出很好的例子,因为你不太可能在架构层面上实现任何新的或深刻的东西;这都是关于低级细节的。 (我估计“在野外”大量使用位操作都是过早优化的例子。)Well, the official Java tutorial Bitwise and Bit Shift Operators covers the actual operations that are available in Java, and how to invoke them.
If you're wondering "what can I do with bit-shifting", then that's not Java specific, and since it's a low-level technique I'm not aware of any list of "cool things you can" do per se. It'd be worth becoming familiar with the definitions, and keeping your eyes open for other code where this is used, to see what they've done.
Note that often bit-twiddling is an efficiency gain at the expense of clarity. For example,
a << 1
is usually the same asa * 2
but arguably less clear. Repeated XORs can swap two numbers without using a temporary variable, but it's generally considered better form to write the code more clearly with the temporary variable (or even better, in a utility method). So in this respect it's hard to give great examples, because you're not likely to achieve anything new or profound on an architecture level; it's all about the low-level details. (And I'd estimate that a vast number of uses of bit-twiddling "in the wild" are instances of premature optimisation.)