How to use bitshifting in Java

百般思念 提交于 2019-12-02 10:45:12
(byte) (4 << 4) | 5

This shifts the value 4 to the left, then sets lower 4 bits to the value 5.

  1. 00000100 A value (4)
  2. 01000000 After shifting left 4 bits (<< 4)
  3. 00000101 Another value (5)
  4. 01000101 The result of a bitwise OR (|) of #2 and #3

Because the operands are int types (and even if they were byte values, they'd be promoted to int when operators like | act on them), the final result needs a cast to be stored in a byte.

If you are using byte values as operands in any bitwise operations, the implicit conversion to int can cause unexpected results. If you want to treat a byte as if it were unsigned in that conversion, use a bitwise AND (&):

byte b = -128; // The byte value 0x80, -128d
int uint8 = b & 0xFF; // The int value 0x00000080, 128d
int i = b; // The int value 0xFFFFFF80, -128d
int uintr = (b & 0xFF) | 0x04; // 0x00000084
int sintr = b | 0x04; // 0xFFFFFF84

You can do something like this:

    int a = 0x04;
    a <<= 4;
    a |= 0x05;
    System.out.println(a);

which essentially turns 0b00000100 into 0b01000000, then into 0b01000101.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

To make a compact field containing both Version and IHL in one byte, try doing

byte b = (byte)((Version << 4) + IHL);

This will only work if Version and IHL are numbers from 0 to 15

Just because a byte is 8 bits and your values can only be a maximum of 4 is not a problem. The extra 4 bits will just always be zeroes.

So if you were storing 1 for example:

0000 0001

or 15 (which is the maximum value right?):

0000 1111

Faiz Halde

Byte shifting is not possible in Java.

How does bitshifting work in Java?

However, as far as the logic is concerned, if you want the version and IHL in one byte, you could do it using the following

byte value = (byte) (IHL | VERSION << 4);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!