English 中文(简体)
利用工会调整C bitfield
原标题:Forcing alignment of C bitfield using a union

我很想知道,是否有可能强行调整C的比照点。 采用以下代码中的变量,我知道,从_align_bytes,然后从bits读到,由于执行取决于,没有界定(和反之亦然)。 下面的代码是否是一种有效的方法,用以“确保”bits在不签名的短程中同时储存? 我认为(无论最终问题如何),这一法典是正确的......但禁区和工会是我最不熟悉的两个C专题。

我正在开展一个低水平的微观控制器项目,希望一种容易的阅读配置比照方法,没有一吨光罩。 感谢提出任何建议。

Sam

P.S. 请忽略我对结束性所作的任何假设,因为我正在从事的项目水平很低,并非打算运往其他装置/平台。

#include <stdio.h>
#include <assert.h>

typedef union packet {
    struct {
        unsigned int bit0  : 1;
        unsigned int bit1  : 1;
        unsigned int bit2  : 1;
        unsigned int bit3  : 1;
        unsigned int bit4  : 1;
        unsigned int bit5  : 1;
        unsigned int bit6  : 1;
        unsigned int bit7  : 1;
        unsigned int bit8  : 1;
        unsigned int bit9  : 1;
        unsigned int bit10 : 1;
        unsigned int bit11 : 1;
        unsigned int bit12 : 1;
        unsigned int bit13 : 1;
        unsigned int bit14 : 1;
        unsigned int bit15 : 1;
    } bits;

    unsigned short _align_bytes;
} packet_t;

int main(int argc, char *argv[]) {

    assert(sizeof(unsigned short) == 2);

    unsigned short data = 0xA05F;
    packet_t *p = (packet_t *)&data;

    printf("%u", p->bits.bit15);
    printf("%u", p->bits.bit14);
    printf("%u", p->bits.bit13);
    printf("%u", p->bits.bit12);
    printf("%u", p->bits.bit11);
    printf("%u", p->bits.bit10);
    printf("%u", p->bits.bit9);
    printf("%u", p->bits.bit8);
    printf("%u", p->bits.bit7);
    printf("%u", p->bits.bit6);
    printf("%u", p->bits.bit5);
    printf("%u", p->bits.bit4);
    printf("%u", p->bits.bit3);
    printf("%u", p->bits.bit2);
    printf("%u", p->bits.bit1);
    printf("%u", p->bits.bit0);

    return 0;
}
问题回答

这是一种共同的模式,我知道,答案是肯定的:轨道领域是毗连的,与“主权”领域一样记忆。 这是整个工会、权利? 寻找同一记忆的不同方法。

我不肯定你所说的话,即“写到......align_bytes,然后从借方读到什么是没有定义的”。 我看到的唯一问题是终点:轨道0可能是 l或 _。 如果你不希望它能够运输,那么你就只需要迅速测试,以说明它是什么样子,并且应该确定。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...