如何查看 Short 的位表示?
如果我将其作为指向内存的指针作为指向短裤的指针:
unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
并且我知道 ms 的大小(该短裤的数量),我希望看到运行所有这些短裤及其二进制表示形式。
如何在 C++ 中访问每个短路的位?
If I have this as pointer to memory as a pointer to shorts:
unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
and I know the size of ms (number of this shorts), I would like to see running through all these shorts and their binary representation.
How can I access the bits of each short in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您假设
unsigned Short
有 16 位,那么您可以通过按位运算检索它们中的每一个:If you are working under the assumption that an
unsigned short
has 16 bits, then you can retrieve each of them with bitwise operations:由于
_memory
指向 Shorts 列表,因此您的ms
指针可以用作数组。As
_memory
points to a list of shorts yourms
pointer can be used as an array.尝试通过位集
编辑(添加代码)
或
try through bitset
EDIT(added code)
or
其中,
N_SHORTS_IN_BUFFER
是内存
中的短路数量。short
中的位数为CHAR_BIT * sizeof(short)
。where
N_SHORTS_IN_BUFFER
is the number of shorts inmemory
.The number of bits in a
short
isCHAR_BIT * sizeof(short)
.要查看
T
类型的任何变量的二进制表示,您可以执行如下操作:您可以将其插入您的短裤列表中。
(您甚至可以将
printf
替换为索引p[i] / 16
和p[i] % 16
中的两次查找合适的字母表:或者用真正的二进制打印机替换它:
您可以将其链接到前一个循环中,而不是两个
printf
调用。)To see the binary representation of any variable of type
T
, you can do something like this:You can plug this into your list of shorts.
(You could even replace
printf
by two lookups of indexp[i] / 16
andp[i] % 16
in a suitable alphabet:Or replace it by a genuine binary printer:
You can chain that into the previous loop instead of the two
printf
calls.)