如何将一个字节和4位从另一个字节转换为使用位的短字节?

发布于 2025-01-26 07:13:09 字数 490 浏览 3 评论 0原文

使用Bitwise,我们如何以最具性能的方式将这3个字节转换为两条短裤?:
(1111111)(01111110)(10000001)3字节
(111111110111)(111010000001)2个短裤
找到了一种将两个字节组合为简短的方法,但是对于1个字节和4位组合,尝试了多种方法,但没有成功。感谢您的帮助。

byte byt1 = -1; // 11111111
byte byt2 = 126;// 01111110
byte byt3 = 129;// 10000001
short s_1_2 = (short) ((byt1 << 8) | (byt2 & 0xFF));
// value is 1111111101111110
short s1 = // want to be 111111110111
short s2 = // want to be 111010000001

Using bitwise, how could we convert these 3 bytes to two shorts in this pattern in the most performant way?:
(11111111)(01111110)(10000001) 3 bytes
(111111110111)(111010000001) 2 shorts
Found a way to combine two bytes into a short, but for a combination of 1 byte and 4 bits tried a variety of ways for hours with no success. Thanks for your help.

byte byt1 = -1; // 11111111
byte byt2 = 126;// 01111110
byte byt3 = 129;// 10000001
short s_1_2 = (short) ((byt1 << 8) | (byt2 & 0xFF));
// value is 1111111101111110
short s1 = // want to be 111111110111
short s2 = // want to be 111010000001

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

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

发布评论

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

评论(1

仙女山的月亮 2025-02-02 07:13:09

可能性之一是以下内容:

short s1 = (short) (((byt1 & 0xff) << 4) | ((byt2 & 0xf0) >> 4));
short s2 = (short) (((byt2 & 0x0f) << 8) | (byt3 & 0xff));

One of the possibilities is the following:

short s1 = (short) (((byt1 & 0xff) << 4) | ((byt2 & 0xf0) >> 4));
short s2 = (short) (((byt2 & 0x0f) << 8) | (byt3 & 0xff));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文