如何查看 Short 的位表示?

发布于 2024-12-15 17:40:44 字数 208 浏览 7 评论 0原文

如果我将其作为指向内存的指针作为指向短裤的指针:

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 技术交流群。

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

发布评论

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

评论(5

染墨丶若流云 2024-12-22 17:40:47

如果您假设 unsigned Short 有 16 位,那么您可以通过按位运算检索它们中的每一个:

for( unsigned short* iter = ms; iter != ms + num_of_ushorts; ++iter )
{
    int bitN = ( *iter ) & ( 1 << N ); // get the N bit
}

If you are working under the assumption that an unsigned short has 16 bits, then you can retrieve each of them with bitwise operations:

for( unsigned short* iter = ms; iter != ms + num_of_ushorts; ++iter )
{
    int bitN = ( *iter ) & ( 1 << N ); // get the N bit
}
兔小萌 2024-12-22 17:40:47

由于 _memory 指向 Shorts 列表,因此您的 ms 指针可以用作数组。

unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);

for (int i = 0; i < NUM_SHORTS_IN_MEM; i++)
    cout << i << "th element\t" << ms[i] << endl;

As _memory points to a list of shorts your ms pointer can be used as an array.

unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);

for (int i = 0; i < NUM_SHORTS_IN_MEM; i++)
    cout << i << "th element\t" << ms[i] << endl;
神也荒唐 2024-12-22 17:40:46
cout << "\t" << dec << x << "\t\t Decimal" << endl;
cout << "\t" << oct << x << "\t\t Octal" << endl;
cout << "\t" << hex << x << "\t\t Hex" << endl;
cout << "\t" << bitset<MAX_BITS>(x) << endl;

尝试通过位集

编辑(添加代码)

#include <iostream>
#include <bitset>
using namespace std;

int main( int argc, char* argv[] )
{
  unsigned short _memory[] = {0x1000,0x0010,0x0001};
  unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
  for(unsigned short* iter = ms; iter != ms + 3/*number_of_shorts*/; ++iter )
  {
    bitset<16> bits(*iter);
    cout << bits << endl;
    for(size_t i = 0; i<16; i++)
    {
      cout << "Bit[" << i << "]=" << bits[i] << endl;
    }
    cout << endl;
  }
}

#include <iostream>
#include <algorithm>
#include <bitset>
#include <iterator>

int main( int argc, char* argv[] )
{
    unsigned short _memory[] = {0x1000,0x0010,0x0001};
    unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
    unsigned int num_of_ushorts = 3;//

    std::copy(ms, ms+num_of_ushorts, ostream_iterator<bitset<16>>(cout, " "));
}
cout << "\t" << dec << x << "\t\t Decimal" << endl;
cout << "\t" << oct << x << "\t\t Octal" << endl;
cout << "\t" << hex << x << "\t\t Hex" << endl;
cout << "\t" << bitset<MAX_BITS>(x) << endl;

try through bitset

EDIT(added code)

#include <iostream>
#include <bitset>
using namespace std;

int main( int argc, char* argv[] )
{
  unsigned short _memory[] = {0x1000,0x0010,0x0001};
  unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
  for(unsigned short* iter = ms; iter != ms + 3/*number_of_shorts*/; ++iter )
  {
    bitset<16> bits(*iter);
    cout << bits << endl;
    for(size_t i = 0; i<16; i++)
    {
      cout << "Bit[" << i << "]=" << bits[i] << endl;
    }
    cout << endl;
  }
}

or

#include <iostream>
#include <algorithm>
#include <bitset>
#include <iterator>

int main( int argc, char* argv[] )
{
    unsigned short _memory[] = {0x1000,0x0010,0x0001};
    unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
    unsigned int num_of_ushorts = 3;//

    std::copy(ms, ms+num_of_ushorts, ostream_iterator<bitset<16>>(cout, " "));
}
滴情不沾 2024-12-22 17:40:46
for (size_t i=0; i<N_SHORTS_IN_BUFFER; i++)
    // perform bitwise ops

其中,N_SHORTS_IN_BUFFER内存 中的短路数量。

short 中的位数为 CHAR_BIT * sizeof(short)

for (size_t i=0; i<N_SHORTS_IN_BUFFER; i++)
    // perform bitwise ops

where N_SHORTS_IN_BUFFER is the number of shorts in memory.

The number of bits in a short is CHAR_BIT * sizeof(short).

醉南桥 2024-12-22 17:40:44

要查看 T 类型的任何变量的二进制表示,您可以执行如下操作:

template <typename T>
void print_raw(const T & x)
{
  const unsigned char * const p = reinterpret_cast<const unsigned char *>(&x);
  for (std::size_t i = 0; i != sizeof(T); ++i)
  {
    if (i != 0) std::putchar(' ');
    std::printf("%02X", p[i]);
  }
}

您可以将其插入您的短裤列表中。

(您甚至可以将 printf 替换为索引 p[i] / 16p[i] % 16 中的两次查找合适的字母表:

static const char alphabet = "01234567890ABCDEF";
std::putchar(alphabet[p[i] / 16]);
std::putchar(alphabet[p[i] % 16]);

或者用真正的二进制打印机替换它:

void print_byte(unsigned char b)
{
  for (std::size_t i = CHAR_BIT; i != 0; --i)
  {
    std::putchar(b & (1u << (i-1)) ? '1' : '0');
  }
}

您可以将其链接到前一个循环中,而不是两个printf调用。)

To see the binary representation of any variable of type T, you can do something like this:

template <typename T>
void print_raw(const T & x)
{
  const unsigned char * const p = reinterpret_cast<const unsigned char *>(&x);
  for (std::size_t i = 0; i != sizeof(T); ++i)
  {
    if (i != 0) std::putchar(' ');
    std::printf("%02X", p[i]);
  }
}

You can plug this into your list of shorts.

(You could even replace printf by two lookups of index p[i] / 16 and p[i] % 16 in a suitable alphabet:

static const char alphabet = "01234567890ABCDEF";
std::putchar(alphabet[p[i] / 16]);
std::putchar(alphabet[p[i] % 16]);

Or replace it by a genuine binary printer:

void print_byte(unsigned char b)
{
  for (std::size_t i = CHAR_BIT; i != 0; --i)
  {
    std::putchar(b & (1u << (i-1)) ? '1' : '0');
  }
}

You can chain that into the previous loop instead of the two printf calls.)

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