如何在 C 中创建 24 位无符号整数

编程入门 行业动态 更新时间:2024-10-28 02:32:32
本文介绍了如何在 C 中创建 24 位无符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个 RAM 非常紧张的嵌入式应用程序.为此,我需要创建一个 24 位无符号整数数据类型.我正在使用结构来执行此操作:

I am working on an embedded application where RAM is extremely tight. For this purpose I need to create a 24 bit unsigned integer data type. I am doing this using a struct:

typedef struct { uint32_t v : 24; } uint24_t;

但是,当我查询这种类型的变量的大小时,它返回4",即:

However when I interrogate the size of a variable of this type, it returns "4", i.e.:

uint24_t x; x.v = 0; printf("Size = %u", sizeof(x));

有没有办法强制这个变量有 3 个字节?

Is there a way I can force this variable to have 3 bytes?

最初我认为这是因为它强制数据类型按字对齐,但我可以这样做:

Initially I thought it was because it is forcing datatypes to be word aligned, but I can for example do this:

typedef struct { uint8_t blah[3]; } mytype;

在这种情况下,大小为 3.

And in that case the size comes out at 3.

推荐答案

好吧,您可以尝试确保该结构仅占用您需要的空间,例如:

Well, you could try to ensure that the structure only takes up the space you need, with something like:

#pragma pack(push, 1) typedef struct { uint8_t byt[3]; } UInt24; #pragma pack(pop)

您可能必须提供那些编译器指令(如上面的#pragma 行)以确保没有填充,但这可能只有八位字段的结构的默认值(a).

You may have to provide those compiler directives (like the #pragma lines above) to ensure there's no padding but this will probably be the default for a structure with only eight-bit fields(a).

然后您可能需要在结构中打包/解包实际值,例如:

You would probably then have to pack/unpack real values to and from the structure, something like:

// Inline suggestion used to (hopefully) reduce overhead. inline uint32_t unpack(UInt24 x) { uint32_t retVal = x.byt[0]; retval = retVal << 8 | x.byt[1]; retval = retVal << 8 | x.byt[2]; return retVal; } inline UInt24 pack(uint32_t x) { UInt24 retVal; retVal.byt[0] = (x >> 16) & 0xff; retVal.byt[1] = (x >> 8) & 0xff; retVal.byt[2] = x & 0xff; return retVal }

请注意,无论您的实际架构如何,这都会为您提供大端值.如果您只是自己打包和解包,这无关紧要,但是如果您想在 特定 布局中的其他位置使用内存块(在在这种情况下,您可以更改打包/解包代码以使用所需的格式).

Note that this gives you big-endian values regardless of your actual architecture. This won't matter if you're exclusively packing and unpacking yourself, but it may be an issue if you want to use the memory blocks elsewhere in a specific layout (in which case you can just change the pack/unpack code to use the desired format).

此方法会向您的系统添加一些代码(并且可能会造成最小的性能损失),因此您必须决定是否值得节省所使用的数据空间.

This method adds a little code to your system (and a probably minimal performance penalty) so you'll have to decide if that's worth the saving in data space used.

(a) 例如,对于以下程序,gcc 7.3 和 clang 6.0 都显示 3 6,表明结构内或结构后没有填充:

(a) For example, both gcc 7.3 and clang 6.0 show 3 6 for the following program, showing that there is no padding either within or following the structure:

#include <stdio.h> #include <stdint.h> typedef struct { uint8_t byt[3]; } UInt24; int main() { UInt24 x, y[2]; printf("%zd %zd\n", sizeof(x), sizeof(y)); return 0; }

然而,那是只是一个示例,因此为了可移植代码的利益,您可能需要考虑使用诸如 #pragma pack(1) 之类的东西,或者将在代码中捕获可能不是这种情况的环境.

However, that is just a sample so you may want to consider, in the interest of portable code, using something like #pragma pack(1), or putting in code to catch environments where this may not be the case.

更多推荐

如何在 C 中创建 24 位无符号整数

本文发布于:2023-11-03 06:58:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554508.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   符号   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!