I was seeing an example reading bits from a byte and the implementation was simple and easy to understand. I was thinking that anyone has a similar example of inserting bits into a byte or byte array, it is easy to understand and also applies like the example below.
There is a byte reading bits:
Fixed bit beats 3 (byte b, int offset, ant count) {return (b & gt; & gt; offset ) & Amp; ((1 & lt; c; count;) - 1); }
What I'm trying to do here is my current implementation, I'm a bit confused with bit-masking / transfer etc, so I'm trying to figure out What is an easy way to do that
BYTE message [2]; Msg_Id = 3; Msg_Event = 1; Msg_Ready = 2; Message [0] = ((Msg_Event <4) & amp; 0xF0) | (MSS_ID & amp; 0x0F); Message [1] = message_radi & amp; 0x0F; // MsgReady & amp; Unused If you are continuously using consecutive constant values like the above example, then you should move bits with these constants Put them in the time of a byte. Otherwise they overlap: In your example, Msg_Id
is equal to Msg_Event & amp; Msg_Ready
. It can be used
the message [0] = (1
(Note that the bits inside a byte are indexed from 0.) The second method will use the powers of constant values of 2:
Msg_Id = 4; // equal 1 & lt; & Lt; 2 Msg_Event = 1; // equal 1 & lt; & Lt; 0 Msg_Ready = 2; // equal 1 & lt; & Lt; 1
Note that in your code above, there is no need for masking with 0x0F
or 0xF0 : (Msg_Id & amp; 0x0F) == Msg_Id
and ((Msg_Event .
Comments
Post a Comment