Git Product home page Git Product logo

Comments (4)

sea-boat avatar sea-boat commented on June 24, 2024

因为存在二进制补码问题

from mysql-protocol.

CallMeJiaGu avatar CallMeJiaGu commented on June 24, 2024

是的,的确存在二进制补码的问题。但是这个问题应该是发生在低位转高位的时候。
下面将byte转为long的时候,考虑到补码的问题,所以需要&0xff。

public long readUB4() {
		final byte[] b = this.data;
		long l = (long) (b[position++] & 0xff);
		l |= (long) (b[position++] & 0xff) << 8;
		l |= (long) (b[position++] & 0xff) << 16;
		l |= (long) (b[position++] & 0xff) << 24;
		return l;
	}

但是像下面这样的高位转低位也存在补码的问题吗?

public static final void writeUB3(ByteBuffer buffer, int i) {
		buffer.put((byte) (i & 0xff));
		buffer.put((byte) (i >>> 8));
		buffer.put((byte) (i >>> 16));
	}

from mysql-protocol.

sea-boat avatar sea-boat commented on June 24, 2024

你好,你说的是高到低没有问题的,此时写上0xff就是看着明确点

from mysql-protocol.

CallMeJiaGu avatar CallMeJiaGu commented on June 24, 2024

好的,谢谢你的解答

from mysql-protocol.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.