Skip to content

进制转化的一些疑惑 #6

@CallMeJiaGu

Description

@CallMeJiaGu
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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions