1
votes

In reading the WebSocket protocol standard, I came across the following passage:

The length of the "Payload data", in bytes: if 0-125, that is the payload length. If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length. If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0) are the payload length. Multibyte length quantities are expressed in network byte order.

I have two questions:

  1. What is meant by "the most significant bit must be 0"? My understanding is that the 1 is more significant than 0, so the only number whose most significant bit is 0 would be 0 itself. Obviously, this can't be right. What does this actually mean?
  2. What is meant by "multibyte length quantities are expressed in network byte order"? I'm assuming this is to say that 2-byte and 8-byte payload lengths are expressed in network byte order, but what is network byte order?
1
The most significant bit is the one that changes the number the most when flipped – the leftmost. Network byte order is big-endian.Ry-
@Ryan thanks, that helpsJack

1 Answers

3
votes

Most significant bit refers to the bit in the largest place-value position in a bit sequence that represents a number. Consider a four-bit sequence. Here, if you limit the most significant bit to 0, resultant 4-bit numbers will look like 0XXX, where each X can be either a 0 or a 1.

In the above documentation, the payload length field is marked as 127 when you have a longer payload whose length requires more than 2 bytes (16 bits) to express. In this case, the 127 (or 1111111) in the payload field, does not indicate the length, but instead acts as a flag to indicate that the following eight bytes are all part of the payload field. It is indicated that the most significant bit out of these eight bytes must be 0. That means the leftmost bit on the payload field must be set to 0 (i.e. bit 16 in the frame).

Network Byte Order refers to the big-endian system of encoding, as it is used in network transmission.