Java - 用整数和字节进行位移 [英] Java - bit shifting with integers and bytes

查看:186
本文介绍了Java - 用整数和字节进行位移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码(其中byteIndex是int):

Consider the following code (where byteIndex is an int):

int bitNumber = b-(8*byteIndex);
bitMask = 0x8>>(byte)bitNumber;

这会产生错误

error: possible loss of precision

编译时(必需字节,找到int )。

when compiled (required byte, found int).

代码

int bitNumber = b-(8*byteIndex);
bitMask = 0x8>>2;

编译好。

什么是问题在这里,如何修复第一个允许按int值移位的示例?

What is the problem here and how do I fix the first example to allow bit shifting by the int value?

编辑:在评论之后,这是一个更完整的例子:

Following the comments, here is a more-complete example:

48) int byteIndex;
49) byte bitMask;
50) int bitNumber;
    // assign value to byteIndex
67) bitNumber = b-(8*byteIndex);
68) bitMask = 0x8>>bitNumber;

并且给出的错误是:

...MyClass.java:68: error: possible loss of precision
    bitMask = 0x8>>bitNumber;
             ^
  required: byte
  found:    int
1 error


推荐答案

转移行转换为: -

byte bitMask = (byte)(0x8>>(byte)bitNumber);

您的RHS是一个int,您需要将其强制转换为字节..

Your RHS, is an int, you need to typecast it to byte..

上面的代码可以正常工作..有或没有铸造 bitNumber 字节

Above code will work fine.. With or without the casting of bitNumber to byte

所以,你也可以: -

So, you can also have : -

byte bitMask = (byte)(0x8>>bitNumber);

但是,这是一个问题 - 字节bitMask = 0x8>> 3 ; 工作正常..为什么会这样?

But, here's is a question - byte bitMask = 0x8>>3; works fine.. Why is it so??

这里有一些例子来解释其工作背后的原因以及行为 final : -

Here's some example to explain the reason behind its working and also the behaviour with final: -

byte bitMask;
int varInt1 = 3;
final int finalVarInt2 = 3;
final int finalVarInt3 = 4;

bitMask = 0x8>>varInt1;    // 1. Will not work. 
bitMask = 0x8<<3;          // 2. Will work

bitMask = 0x8<<4;          // 3. Will not work
bitMask = 0x8<<finalVarInt2;   // 1. Will work
bitMask = 0x8<<finalVarInt3;   // 2. Will not work

以下是解释上述行为的一些推理: -


  • RHS上的值将是 typecasted 隐含地仅在以下情况下:编译器确定,它能够在LHS上的 byte 变量中容纳该值。否则,我们必须做显式类型转换告诉编译器,我们知道我们在做什么,只为我们做..

  • The value on the RHS will be typecasted implicitly only if, the compiler is sure that, it will be able to accomodate that value in the byte variable on LHS.. Else, we have to do Explicit type casting to tell compiler that, we know what we are doing, just do it for us..

现在让我们逐一考虑所有案例(来自上面的代码(1-3,1-2): -


  1. varInt1 最初包含 3 。因此 RHS 的值会评估 64 。虽然此值可能适用于 LHS 中的 byte 变量,但编译器也知道,有可能改变 varInt1 的值..那么如果va怎么办? l strong varInt1 在某个阶段更改为 4 ..它将无法正常工作..这就是为什么它不允许..

  2. 现在,在这种情况下,因为我们已经明确使用了 Integer Literal 在这里,所以编译器确保它可以容纳 byte ..所以它允许 隐式 施放..

  3. 同样,在这种情况下,已知 RHS 将评估为 128 byte .. 再次失败 ..

  1. varInt1 initially contains 3. So the value of RHS evaluates to 64. Although this value might get accomodated to byte variable in LHS, but compiler also knows that, it is possible to change the value of varInt1.. So what if value of varInt1 is changed to 4 at some stage.. It won't work then.. That's why it is not allowed..
  2. Now, in this case, since we have explicitly used an Integer Literal here, so compiler is sure that it will accomodate in byte.. So it allows the implicit casting..
  3. Again, in this case, it is known that RHS will evaluate to 128 which can't be accomodated in byte.. Failed again..

最后两种情况与常规变量不同...因为它们被声明为 final ,所以它们无法重新初始化。所以,编译器可以根据指定的值做出决定..

Last two cases are different from regular variables... Since they are declared final, they can't be re-initialized.. So, compiler can make a decision based on the assigned value..


  1. 在这种情况下,编译器会看到, finalVarInt2 包含值 3 。因此, RHS 评估为 64 ,可以在 字节 变量中进行调整的 LHS 即可。现在,由于变量 final ,因此无法更改,编译器知道那么,确定t * 他的值总是 64 * ..所以编译器允许这样做。

  1. In this case, compiler sees that, finalVarInt2 contains value 3. So, RHS evaluates to 64, which can be accommodated in the byte variable on LHS. Now, since the variable is final it can't be changed, and Compiler knows that, so it is sure that t*his value will always be 64*.. So compiler allows this.

在最后一种情况下, finalVarInt3 的值是4 ..类似的推理..不会适合 LHS ,因为 RHS 评估为 128 ,这不符合 字节

In the last case, value of finalVarInt3 is 4.. Similar reasoning.. Won't fit in LHS, as RHS evaluates to 128 which can't fit into byte

这篇关于Java - 用整数和字节进行位移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆