Assignment of value in parts

If I have a structure like this:

struct example
{
   uint16_t bar:1;
   uint16_t foo:1;
};

Then I have another 16-bit variable: uint16_t value;

If I want the first bit in the "value" to be assigned to "example.bar" and the second bit in the value to be assigned to "example.foo", how would I do it?

EDIT:

I tried the following:

typedef struct
{
    unsigned short      in_1_16;
    unsigned short      in_17_32;
    unsigned short      in_33_48;
} READSTR;

typedef struct 
{   
    unsigned short      chan_1_16;
    unsigned short      chan_17_32;
    unsigned short      chan_33_48; 
} TLM;

void main()
{
    TLM tlm;
    tlm.chan_1_16 = 0xFFFF;
    READSTR t675;

    t675.in_1_16 = 0x10;
    t675.in_17_32 = 0x9;
    t675.in_33_48 = 0x8;

tlm.chan_1_16 |= t675.in_1_16 & 1;
tlm.chan_1_16 |= t675.in_1_16 & (1 << 1);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 2);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 3);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 4);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 5);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 6);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 7);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 8);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 9);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 10);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 11);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 12);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 13);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 14);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 15);
printf("%x", tlm.chan_1_16);

So, I set the value of in to struct for all (0xFFFF), and I'm trying to set it to 0x10, bit by bit. But when I run this code, I still get 0xFFFF. Not sure what I am doing wrong?

+3
source share
4 answers

try:

struct example x;
x.bar = value & 1;
x.foo = value & 1 << 1;

fooN = value & 1 << N;
0
source

It is impossible to say, because bit fields are very poorly defined by the standard. Below you may not know:

  • whether int bit field is used as signed or unsigned int
  • (lsb ?)
  • CPU .
  • ( ?)
  • endianess
  • int
  • ..

, , , - -. - :

#define FOO 0x01U
#define BAR 0x02U

uint16_t value;

value |= FOO; /* set FOO bit */
value &= ~FOO; /* clear FOO bit */

C/++ , .

+3
#include <iostream>

typedef unsigned short uint16;

struct example
{
   uint16 bar:1;
   uint16 foo:1;
};

union foo
{
    struct example x;
    uint16 value;
};

int main()
{
    uint16 bar = 0x01;

    foo f;
    f.value = bar;

    std::cout << f.x.bar << std::endl; // display 1
    std::cout << f.x.foo << std::endl; // display 0
}


, . , static assert unit-, . , , " ".

(OP) ++, std::bitset:

#include <bitset>

int main()
{
    std::bitset<16> set(bar);

    example x;
    x.bar = set[0];
    x.foo = set[1];

    std::cout << x.bar << std::endl; // display 1
    std::cout << x.foo << std::endl; // display 0
}
+1

, , 2 ** N, N - . , , N .

So, considering struct example xif you want to place the least significant bit valuein x.bar, you can simply do:

x.bar = value;

If you want the second minor bit to valuebe put in x.foo, you can also use:

x.foo = value >> 1;
0
source

Source: https://habr.com/ru/post/1789520/


All Articles