Some bitshifting help needed

Hello,

I need a little programming help.

I have a 16bit integer, and i would like to convert it to two 8 bit integers

Here is the function, but this doesn't work well, could anybody help me?

void SendPitchBend(uint8_t Ch,uint16_t Val) 
{
  MidiMsg Message;
  Message.D1=0xE;
  Message.D1=((Message.D1<<4)|Ch);
  
  Message.D2=Val;
  Message.D3=Val>>8;
  
  SendMidiMsg(&Message);
}

But not just separating them, here is an example:

4240 = 0001 0000 1001 0000

D2 should be 0010 0001
D3 should be 0001 0000

The MidiMSG structure looks like this:

struct MidiMsg
{
  union
  {
    struct { 
      uint8_t D1, D2, D3; 
    };
    uint8_t Raw[3];
  };
};

Problem solved

Problem solved

Good. I guess it doesn't really matter what the problem was or how you solved it.

The problem was My stupidity (confusing MSB and LSB), and here is the solution:

void SendPitchBend(uint8_t Ch,uint16_t Val) 
{
  MidiMsg Message;
  Message.D1=0xE<<4|Ch;
  Message.D2=Val<<1;
  Message.D2=Message.D2>>1;
  Message.D3=Val>>7;
  SendMidiMsg(&Message);
}

Maybe the part where Message.D2 gets its value, could be done in 1 command, but i haven't figured it out how

Also, this method possibly won't work in all situations, because my initial 16 bit integer contains only max. 14 bit numbers, and D2&D3 should represent that number in two 7 bit parts, the MSB in each byte will be always 0.

This is a soluion for sending pitch bend data over midi, and it is a bit overcomplicated i think, because the Pitch Bend message includes two data bytes to specify the pitch bend value. The two bytes of the pitch bend message form a 14 bit number ( 0 to 16383 ).

EDIT: The part where I give value to Message.D2 possibly could be done like this:

  Message.D2=Val & 0x7F;

I found this solution in an already existing MIDI library, but haven't tested it yet.

The problem was My stupidity (confusing MSB and LSB)

It's not stupidity unless you persist in not learning. Seems to me that you did learn something.