在 C++ 中附加两个无符号字符的位运算符

发布于 2024-07-08 18:40:20 字数 272 浏览 9 评论 0原文

如果我有两个十六进制的东西,我可以如何将它们的二进制文件附加在一起以获得值吗?

在 C++ 中, 说我有

unsigned char t = 0xc2;  // 11000010
unsigned char q = 0xa3;  // 10100011

我想要的东西, 1100001010100011,使用按位运算符可以吗?

我想提取 t 和 q 的二进制形式并附加它们......

If I have two things which are hex, can I someone how append their binary together to get a value?

In C++,
say I have

unsigned char t = 0xc2;  // 11000010
unsigned char q = 0xa3;  // 10100011

What I want is somehow,
1100001010100011, is this possible using bit-wise operators?

I want to extract the binary form of t and q and append them...

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

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

发布评论

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

评论(6

樱桃奶球 2024-07-15 18:40:20

是的,这是可能的。

只需使用左移运算符,向左移位 8,至少使用 16 位整数。 然后将第二个值与整数进行二进制或运算。

unsigned char t = 0xc2; // 11000010 
unsigned char q = 0xa3; // 10100011
unsigned short s = (((unsigned short)t)<<8) | q; //// 11000010 10100011

或者,将两个值放入包含 2 个字符的联合中(注意大端或小端)将得到相同的位级结果。 另一种选择是 char[2]。

Yes it's possible.

Just use the left-bitshift operator, shifting to the left by 8, using at least a 16-bit integer. Then binary OR the 2nd value to the integer.

unsigned char t = 0xc2; // 11000010 
unsigned char q = 0xa3; // 10100011
unsigned short s = (((unsigned short)t)<<8) | q; //// 11000010 10100011

Alternatively putting both values in a union containing 2 chars (careful of big endian or small) would have the same bit level result. Another option is a char[2].

断爱 2024-07-15 18:40:20

连接两个字符:

unsigned char t = 0xc2;  // 11000010
unsigned char q = 0xa3;  // 10100011

int result = t;  // Put into object that can hold the fully concatenated data;
result <<= 8;     // Shift it left
result |= q;     // Or the bottom bits into place;

Concatenating two chars:

unsigned char t = 0xc2;  // 11000010
unsigned char q = 0xa3;  // 10100011

int result = t;  // Put into object that can hold the fully concatenated data;
result <<= 8;     // Shift it left
result |= q;     // Or the bottom bits into place;
影子是时光的心 2024-07-15 18:40:20

您的示例实际上效果不太好,因为未定义输入值的宽度(通常为 8 位)。 例如,为什么你的示例不是:0000000100000010,这将真正按位附加 1 (00000001) 和 2 (00000010)。

如果每个值都有固定宽度那么可以通过位移和ORing值来回答

编辑:如果您的“宽度”定义为删除所有前导零的全宽度,那么可以这样做与移位和 ORing,但更复杂。

Your example doesn't really work too well because the width (usually 8-bits) of the input values aren't defined. For example, why isn't your example: 0000000100000010, which would be truly appending 1 (00000001) and 2 (00000010) bit wise.

If each value does have a fixed width then it can be answered with bit shifting and ORing values

EDIT: if your "width" is defined the full width with all leading zero's removed, then it is possible to do with shifting and ORing, but more complicated.

演出会有结束 2024-07-15 18:40:20

我会选择 char 数组。

无符号短 s;
char * spPtr = &s;
sPtr[0] = t; sPtr[1] = q;

这并不真正关心字节序..
我不确定你为什么要这样做,但这会起作用。

位方法的问题是你不确定你有什么大小。
如果你知道尺寸..我会同意布莱恩的回答

I'd go with the char array.

unsigned short s;
char * sPtr = &s;
sPtr[0] = t; sPtr[1] = q;

This doesn't really care about endian..
I'm not sure why you'd want to do this but this would work.

The problem with the bit methods are that you're not sure what size you've got.
If you know the size.. I'd go with Brians answer

懵少女 2024-07-15 18:40:20

二进制/十六进制中没有附加,因为您正在处理数字(您可以附加 1 和 2 并且不会将结果 12 与“真实”12 混淆吗?)

您可以使用一些特殊符号来分隔它们,但不能只是“连接”它们。

There is no append in binary/hex because you are dealing with Numbers (can you append 1 and 2 and not confuse the resulting 12 with the "real" 12?)

You could delimit them with some special symbol, but you can't just "concatenate" them.

与他有关 2024-07-15 18:40:20

作为操作追加对于数字来说并没有真正的意义,无论它们的基数是什么。 作为串联运算符:在您的示例中为 0x1 。 如果连接十六进制,则 0x2 变为 0x12;如果连接二进制,则变为 0b101。 但 0x12 和 0b101 不是相同的值(以 10 为基数,它们分别是 18 和 5)。 一般来说,无论您使用什么基数进行操作,AOB(其中 A 和 B 是数字,O 是运算符)都应该产生相同的值。

Appending as an operation doesn't really make sense for numbers, regardless of what base they're in. Using . as the concatenation operator: in your example, 0x1 . 0x2 becomes 0x12 if you concat the hex, and 0b101 if you concat the binary. But 0x12 and 0b101 aren't the same value (in base 10, they're 18 and 5 respectively). In general, A O B (where A and B are numbers and O is an operator) should result in the same value no matter what base you're operating in.

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