-
Notifications
You must be signed in to change notification settings - Fork 37
Open
Description
public static final void writeUB3(ByteBuffer buffer, int i) {
buffer.put((byte) (i & 0xff));
buffer.put((byte) (i >>> 8));
buffer.put((byte) (i >>> 16));
}
不明白,为什么int转为byte类型还需要用 i&0xff呢?
我的理解是因为(byte)的强制转化是针对int类型的低8位的,所以就算i没有进行 &0xff的操作,也是不会影响到该值的吧。
所以高字节的int转低字节的byte是不是可以写成:
public static final void writeUB3(ByteBuffer buffer, int i) {
buffer.put((byte) (i) );
buffer.put((byte) (i >> 8));
buffer.put((byte) (i >> 16));
}
Metadata
Metadata
Assignees
Labels
No labels