Java“位移位”教程?

发布于 2024-11-14 04:23:42 字数 1491 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

离去的眼神 2024-11-21 04:23:43

使用移位运算符时,请务必小心,不要重复常见错误!

正如以下 SO post 所示,已接受答案的作者提到:

“在某些语言中,将移位运算符应用于任何小于 int 的数据类型会自动将操作数的大小调整为
int。”

在对字节进行操作时,记住这一点绝对至关重要,否则您可能会得到意想不到的结果(就像我所做的那样)。

给定一个具有以下位模式的字节:

1001 0000

当我尝试按 4 位移位时,并分配给一个 int,例如:

int value = byteValue >>> 4;

我希望有:

0000 1001   (or a value of 9)

但我会得到一个巨大的数字! 那是因为 byteValue 在位移操作之前被转换为 int ,从而导致这样的结果 反而:

1111 1111 1111 1111 1111 1111 1001

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:

"In some languages, applying the shift operators to any datatype smaller than int automatically resizes the operand to be an
int."

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:

1001 0000

When I tried to bit shift by 4, and assigned to an int, such as:

int value = byteValue >>> 4;

I would expect to have:

0000 1001   (or a value of 9)

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:

1111 1111 1111 1111 1111 1111 1001
困倦 2024-11-21 04:23:43

有无数种可能的组合。然而,它们将由一种或多种组合组成。

>> shift right with sign extension.
>>> shift right with out sign extension.
<< shift left.

为了获得理解,我建议您将二进制数写在纸上并计算出会发生什么。尝试在教程中阅读并不能保证理解。尤其是如果他们到目前为止还没有提供帮助的话。

There is an infinite number of possible combinations. However they will be made up of one or more combinations of

>> shift right with sign extension.
>>> shift right with out sign extension.
<< shift left.

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.

就此别过 2024-11-21 04:23:43

我发现这里有一个简单但清晰的教程很有用这里

There is simple but clear tutorial that I find useful here

吃兔兔 2024-11-21 04:23:43

这不完全是一个教程,但我有一个个人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.

乖乖哒 2024-11-21 04:23:43

以下是位移位工作原理的详细信息
有一些官方教程未涵盖的非直观行为。例如,右操作数的范围有限(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.

心奴独伤 2024-11-21 04:23:43

这个网站似乎提供了一个很好的教程,介绍了如何使用位操作(所以不是特定于 java,但因为它很容易翻译)

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.html

上面的教程提供了

  • 按位操作
  • 设置和清除位
  • 显示带位的整数
  • 将十进制转换为十六
  • 进制 整数中设置的位数(个数)
  • 整数的位设置位置
  • 带位操作的就地整数交换
  • 将整数 A 转换为整数 B 所需的位数
  • 交换奇数整数中的偶数位
  • (n & (n-1) == 0) 正在检查什么?
  • 补码
  • 翻转整数的第 n 位
  • 浮点数 位模式
  • 整数的位模式回文

这是一个包含一堆 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

  • Bitwise Operations
  • Setting and Clearing a Bit
  • Displaying an Integer with Bits
  • Converting Decimal to Hex
  • The Number of Bits Set in an Integer (Number of Ones)
  • The Bit Set Position of an Integer
  • In-Place Integer Swap with Bit Manipulation
  • The Number of Bits Required to Convert an Integer A to Integer B
  • Swap Odd and Even Bits in an Integer
  • What (n & (n-1) == 0) is checking?
  • Two's Complement
  • Fliping n-th bit of an integer
  • Floating Point Number Bit Pattern
  • Bit pattern palindrome of an integer

Here's a file that has a bunch of java implementations

http://geekviewpoint.com/

遗失的美好 2024-11-21 04:23:43

这是我在学习位移位时发现的两个很好的教程,它们不是 Java 中的,但大多数语言使用相同的运算符,并且理论是相同的。

  1. 有点小玩意
  2. Jim Plush 的 PHP 位教程

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.

  1. Bit twiddling
  2. PHP Bitwise Tutorial by Jim Plush
岁月蹉跎了容颜 2024-11-21 04:23:42

好吧,官方 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 as a * 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文