位运算中它代表什么?

发布于 2024-10-01 11:51:01 字数 544 浏览 0 评论 0原文

这几行代码代表什么?

payloadType = header[1] & 127;
sequenceNumber = unsigned_int(header[3]) + 256*unsigned_int(header[2]);
timeStamp = unsigned_int(header[7])
               + unsigned_int(header[6])
               + 65536*unsigned_int(header[5])
               + 16777216*unsigned_int(header[4]);

其中 header 是一个 byte[12] ,方法 unisigned_int 是这样的:

private int unsigned_int(byte b) {
    if(b >= 0) {
        return b;
    }
    else {
        return 256 + b;
    }
}

谢谢您的回答!

What do those lines of code stand for?

payloadType = header[1] & 127;
sequenceNumber = unsigned_int(header[3]) + 256*unsigned_int(header[2]);
timeStamp = unsigned_int(header[7])
               + unsigned_int(header[6])
               + 65536*unsigned_int(header[5])
               + 16777216*unsigned_int(header[4]);

Where header is a byte[12] and the method unisigned_int is this:

private int unsigned_int(byte b) {
    if(b >= 0) {
        return b;
    }
    else {
        return 256 + b;
    }
}

Thank you for answering!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小沫ゞ 2024-10-08 11:51:01
 payloadType = header[1] & 127;

从标头 1 中去除符号位/获取底部 7 位

sequenceNumber = unsigned_int(header[3]) + 256*unsigned_int(header[2]);

从标头中提取 16 位值

 timeStamp = unsigned_int(header[7])
           + unsigned_int(header[6])
           + 65536*unsigned_int(header[5])
           + 16777216*unsigned_int(header[4]);

从标头中提取 32 位值。马克·拜尔斯(Mark Byers)观察到了这个错误。

private int unsigned_int(byte b) {
     if(b >= 0) {
         return b;
     }
     else {
         return 256 + b;
     }
}

将 -128 到 127 之间的整数(即一个字节)转换为表示为整数的 8 位无符号 int。相当于

 return b & 255
 payloadType = header[1] & 127;

Strip the sign bit off header 1 / get bottom 7 bits

sequenceNumber = unsigned_int(header[3]) + 256*unsigned_int(header[2]);

extract a 16 bit value from the header

 timeStamp = unsigned_int(header[7])
           + unsigned_int(header[6])
           + 65536*unsigned_int(header[5])
           + 16777216*unsigned_int(header[4]);

extract a 32 bit value from the header. With the bug as observed by Mark Byers.

private int unsigned_int(byte b) {
     if(b >= 0) {
         return b;
     }
     else {
         return 256 + b;
     }
}

convert an integer from -128 to 127 (i.e. a byte) into a 8 bit unsigned int represented as an integer. Equivalent to

 return b & 255
哥,最终变帅啦 2024-10-08 11:51:01

它将字节转换为整数。

我认为这里有一个错误:

+ 256 * unsigned_int(header[6])
  ^^^^^

另外,不要写 x * 256, x * 65536, x * 16777216 ,这样会更清楚写x << 8,x << 16,x << 24

It's converting bytes to integers.

I think there is a bug here:

+ 256 * unsigned_int(header[6])
  ^^^^^

Also instead of writing x * 256, x * 65536, x * 16777216 it would be more clear to write x << 8, x << 16, x << 24.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文