C++访问无符号整数的字节/字的类

发布于 2024-08-06 03:31:21 字数 572 浏览 6 评论 0原文

union LowLevelNumber
{
 unsigned int n;
 struct
 {
  unsigned int lowByte : 8;
  unsigned int highByte : 8;
  unsigned int upperLowByte : 8;
  unsigned int upperHighByte : 8;
 } bytes;
 struct
 {
  unsigned int lowWord : 16;
  unsigned int highWord : 16;
 } words;     
};

这个联合允许我访问无符号整数字节或字。 然而,代码看起来相当难看:

var.words.lowWord = 0x66;

有没有一种方法可以让我编写这样的代码:

var.lowWord = 0x66;

更新:
这实际上是关于编写简短/漂亮的代码,如上面的示例所示。联合解决方案本身确实有效,我只是不想每次访问 lowWord 或 lowByte 时都写入 .words 或 .bytes 。

union LowLevelNumber
{
 unsigned int n;
 struct
 {
  unsigned int lowByte : 8;
  unsigned int highByte : 8;
  unsigned int upperLowByte : 8;
  unsigned int upperHighByte : 8;
 } bytes;
 struct
 {
  unsigned int lowWord : 16;
  unsigned int highWord : 16;
 } words;     
};

This union allows me to access the unsigned integer byte or word-wise.
However, the code looks rather ugly:

var.words.lowWord = 0x66;

Is there a way which would allow me to write code like this:

var.lowWord = 0x66;

Update:
This is really about writing short / beautiful code as in the example above. The union solution itself does work, I just don't want to write .words or .bytes everytime I access lowWord or lowByte.

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

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

发布评论

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

评论(5

仙女 2024-08-13 03:31:22

C++

http://www.cplusplus.com/reference/stl/bitset/ 满足您的需求?

纯 C 版本看起来像这样:

int32 foo;

//...

//Set to 0x66 at the low byte
foo &= 0xffffff00;
foo |= 0x66;

这可能比编写自定义类/联合更易于维护,因为它遵循典型的 C 习惯用法。

C++

Would http://www.cplusplus.com/reference/stl/bitset/ serve for your needs?

Plain C version would look something like this:

int32 foo;

//...

//Set to 0x66 at the low byte
foo &= 0xffffff00;
foo |= 0x66;

This is probably going to be more maintainable down the road than writing a custom class/union, because it follows the typical C idiom.

森末i 2024-08-13 03:31:22

你可以制作

short& loword() { return (short&)(*(void*)&m_source); }

如果你不关心括号,

并使用它。或者您可以选择

public class lowordaccess
{
   unsigned int m_source;
public:
   void assign(unsigned int& source) { m_source = source; }
   short& operator=(short& value) { ... set m_source }
   operator short() { return m_source & 0xFF; }
}

struct LowLevelNumber
{
   LowLevelNumber() { loword.assign(number); }

   unsigned int number;
   lowordaccess loword;
}
var.loword = 1;
short n = var.loword;

一种技术是 C++ 中已知的属性模拟。

You can make

short& loword() { return (short&)(*(void*)&m_source); }

and use it if you don't care parenthesis.

Or you can go fancy

public class lowordaccess
{
   unsigned int m_source;
public:
   void assign(unsigned int& source) { m_source = source; }
   short& operator=(short& value) { ... set m_source }
   operator short() { return m_source & 0xFF; }
}

and then

struct LowLevelNumber
{
   LowLevelNumber() { loword.assign(number); }

   unsigned int number;
   lowordaccess loword;
}
var.loword = 1;
short n = var.loword;

The latter technique is a known property emulation in C++.

九歌凝 2024-08-13 03:31:22

您可以轻松地将其包装在一个类中并使用 get/set 访问器。

You could easily wrap that in a class and use get/set accessors.

心舞飞扬 2024-08-13 03:31:22

为此使用联合是不好的,因为它不可移植于字节顺序。

使用访问器函数并通过位掩码和移位来实现它们。

Using a union for this is bad, because it is not portable w.r.t. endianness.

Use accessor functions and implement them with bit masks and shifts.

东风软 2024-08-13 03:31:21
union LowLevelNumber {
    unsigned int n;
    struct {
        unsigned int lowByte : 8;
        unsigned int highByte : 8;
        unsigned int upperLowByte : 8;
        unsigned int upperHighByte : 8;
    };
    struct {
        unsigned int lowWord : 16;
        unsigned int highWord : 16;
    };
};

请注意删除的 byteswords 名称。

union LowLevelNumber {
    unsigned int n;
    struct {
        unsigned int lowByte : 8;
        unsigned int highByte : 8;
        unsigned int upperLowByte : 8;
        unsigned int upperHighByte : 8;
    };
    struct {
        unsigned int lowWord : 16;
        unsigned int highWord : 16;
    };
};

Note the removed bytes and words names.

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