C 语言中的位域?

发布于 2024-08-30 09:52:59 字数 106 浏览 3 评论 0原文

大家好,

有没有办法我们可以在那些不是结构或联合的任何成员的变量上声明指定位字段的变量。如果没有,那么无论如何我们可以通过指定它的位数来声明变量允许使用。

谢谢 麦迪

HI all,

Is there anyway by which we can declare variable specifying bit fields on those which are not any members of structures or unions.If not,then is there anyway by which we can just declare a variable by specifying the number of bits it is permitted to use.

Thanks
maddy

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

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

发布评论

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

评论(5

黯然 2024-09-06 09:52:59

一种非常简单且古老的技术是只定义一些 #define 变量,其值对应于位位置,然后使用 AND 和 OR 运算来根据需要清除或设置它们。
例如,

#define BIT_0 0x0001
#define BIT_1 0x0002
#define BIT_2 0x0004
#define BIT_3 0x0008
#define BIT_4 0x0010

然后使用它们来设置标准变量中的位位置。 例如,

int someVariable = 0;

someVariable = someVariable | BIT_1; //set bit 1 to 1. someVariable = 2

someVariable = someVariable & ~BIT_1; // clear bit 1. someVariable = 0

效率不高或不聪明,但易于阅读。

编辑 - 添加
如果您希望限制哪些位有效,请设置一个掩码值来应用,如下所示:

#define VALID_BIT_MASK 0x0009 // thus only bits 3 and 0 are valid

作为示例

someVariable = someVariable | BIT_0 | BIT_2 | BIT_4; // someVariable now has value 21

someVariable = someVariable & VALID_BIT_MASK; // remove invalid  bits, someVariable value is now 1

,显然 someVariable 将是 byte、unsigned int 或 unsigned long,但假设您只需要 11 位的无符号数集整数(16 位)。

#define VALID_BIT_MASK 0x07FF; // 011111111111 in binary.

someVariable = someVariable & VALID_BIT_MASK; //strips off unwanted bits.

A really simple and old technique is to just define a number of #define variables whose values correspond to bit locations and then use AND and OR operations to clear or set them as appropriate.
e.g.

#define BIT_0 0x0001
#define BIT_1 0x0002
#define BIT_2 0x0004
#define BIT_3 0x0008
#define BIT_4 0x0010

You then use them to set bit locations in a standard variable e.g.

int someVariable = 0;

someVariable = someVariable | BIT_1; //set bit 1 to 1. someVariable = 2

someVariable = someVariable & ~BIT_1; // clear bit 1. someVariable = 0

Not efficient or clever but easy to read.

edit - added
If you wish to restrict which bits are valid for use then setup a mask value to apply as follows:

#define VALID_BIT_MASK 0x0009 // thus only bits 3 and 0 are valid

As an example

someVariable = someVariable | BIT_0 | BIT_2 | BIT_4; // someVariable now has value 21

someVariable = someVariable & VALID_BIT_MASK; // remove invalid  bits, someVariable value is now 1

Obviously someVariable is going to be byte, unsigned int or unsigned long, but say that you want only 11 bits set of an unsigned int (16 bits).

#define VALID_BIT_MASK 0x07FF; // 011111111111 in binary.

someVariable = someVariable & VALID_BIT_MASK; //strips off unwanted bits.
疯狂的代价 2024-09-06 09:52:59

否 - 除非它恰好与内置类型大小相同(例如 8、16、32 或 64 位),否则您需要将其嵌入到结构中。

No - unless it happens to be the same size as a built-in type (e.g. 8, 16, 32 or 64 bits) then you would need to embed it in a struct.

梦里兽 2024-09-06 09:52:59

不,您应该使用所示的技术 这里

No, you should use the technique shown here

北城孤痞 2024-09-06 09:52:59

使用除内置类型之外的位来声明变量没有任何好处。因为编译器最终会为其保留8、16、32或64位的空间。例如,如果您声明变量 unsigned x:5;然后编译器将创建8位空间来存储它。因为CPU无法读取不是8倍数的内存

there is no benefit in declaring a variable with bits other than built-in type. because compiler will ultimately reserve space for it 8,16,32 or 64 bits.e.g if you declare variable unsigned x:5; then compiler will create 8 bits of space to store it. because CPU can't read memory which is not multiple of 8

自此以后,行同陌路 2024-09-06 09:52:59

在 ARM 环境中,使用 C 语言配置 SOC 的硬件组件时,按位字段操作的使用很常见。
LPC_SC->FLASHCFG = (LPC_SC->FLASHCFG & ~0x0000F000) | FLASHCFG_Val;
使用 FLASHCFG_Val 的值更新配置寄存器中的 4 位字段。
或者, while (!(LPC_SC->PLL1STAT & (1<<10)));//等待PLOCK1
测试状态寄存器中的单个位,其中 1<<10 表示第 10 位位置。

In the ARM context, use of bitwise field operations are common when configuring the hardware components of the SOC using C.
LPC_SC->FLASHCFG = (LPC_SC->FLASHCFG & ~0x0000F000) | FLASHCFG_Val;
updates a 4 bit field in a configuration register with the value of FLASHCFG_Val.
Or, while (!(LPC_SC->PLL1STAT & (1<<10)));// Wait for PLOCK1
to test a single bit in a status register where 1<<10 indicates the 10th bit position.

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