小编Gre*_*rey的帖子

I can't manipulate bit fields in C

I wanted to implement LFSR(linear feedback shift registers) in C to generate random bits. When I try to modify an individual bit or just assign a short value to memory block, all the bits set to 1. How can I stop this from happening?

struct lfsr{
    //... 
    union{
        unsigned short ff_0 : 1;
        unsigned short ff_1 : 1;
        //... 
        unsigned short ff_f : 1;
        unsigned short ff;
    }flip_flops;
};

int main() {
    struct lfsr gen;
    gen.flip_flops.ff = 1;      //all the …
Run Code Online (Sandbox Code Playgroud)

c unions bit-fields

3
推荐指数
1
解决办法
109
查看次数

标签 统计

bit-fields ×1

c ×1

unions ×1