This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Construct a byte from bits

Hello everybody,

I am very new to C language. In my project I get data serially from a port pin. I want to save the first 8 bits to one variable, another to second & so on. In all I want to save 32 bits in 4 bytes. Can you suggest C code.

I know it in assembly by using RRC or RLC, but how to achieve it in C?

Thanks

Parents
  • yes, sorry my mistake was ment to be:

    data_received |= ( 1 << bits_received );
    

    it is the same than:

    data_received = data_received | ( 1 << bits_received );
    

    to split just do a byte *** operation

    BYTE bo, b1, b2, b3;
    b0 = (data_received & 0x000000FF)
    b1 = (data_received & 0x0000FF00) >> 8;
    b2 = (data_received & 0x00FF0000) >> 16;
    b3 = (data_received & 0xFF000000) >> 24;
    

    may need a BYTE cast

Reply
  • yes, sorry my mistake was ment to be:

    data_received |= ( 1 << bits_received );
    

    it is the same than:

    data_received = data_received | ( 1 << bits_received );
    

    to split just do a byte *** operation

    BYTE bo, b1, b2, b3;
    b0 = (data_received & 0x000000FF)
    b1 = (data_received & 0x0000FF00) >> 8;
    b2 = (data_received & 0x00FF0000) >> 16;
    b3 = (data_received & 0xFF000000) >> 24;
    

    may need a BYTE cast

Children