谁能扭转这种方法?

时间:2014-07-20 22:22:39

标签: c bit-manipulation

我正在用这种方法(从这里http://www.blitter.com/~russtopia/MIDI/~jglatt/tech/midispec/wheel.htm)读取midi球轮信息,它将2个十六进制字节组合成一个14位无符号短路。它工作得很好,但现在我试图发出需要采用2字节十六进制格式的音轮消息。有谁知道如何反转这个方法,以便它将采取像12401这样的整数并返回两个字节?

unsigned short CombineBytes(unsigned char First, unsigned char Second)
{
    unsigned short _14bit;
    _14bit = (unsigned short)Second;
    _14bit <<= 7;
    _14bit |= (unsigned short)First;
    return(_14bit);
}

这是我的不良尝试:

unsigned char CreateBytes(unsigned short value)
{
    unsigned char First;
    unsigned char Second;
    unsigned char FullValue;

    FullValue = (unsigned short)value;
    First = FullValue;
    First >>= 7;
    Second |= (unsigned short) value;
    return(First, Second);
}

1 个答案:

答案 0 :(得分:2)

first  = (combined & 0x3f80) >> 7; // 0b11111110000000
second = (combined & 0x007f);      // 0b00000001111111