解释一些简单的C位移码?

No *_*ing 1 objective-c ios5

我需要一些帮助来理解Apple/iOS LoadPresetDemo源代码中的一些简单的C位移位.这很简单,但需要一大堆空间来提供背景,所以感谢与我的关系.

我试图理解程序员为什么使用这一行:

UInt32 noteCommand =  kMIDIMessage_NoteOn << 4 | 0;
Run Code Online (Sandbox Code Playgroud)

程序员已经在代码中的其他地方定义了常量(下面).当我拿出我的计算器并输入0x9并将其向左移动4位时,我得到0x90.我把这个数字插入代码代替代码上面的行运行正常,所以我很困惑.我的两个问题:

  1. 程序员为什么不将常量定义为0x90

  2. 上面代码中OR运算符的用途是什么?

我知道这是一个小而愚蠢的细节,但我想知道这背后是否有一个很好的理由并且理解位移更好一点(没有双关语......).完整的方法和细节如下.- 谢谢!

// some MIDI constants:
enum {
   kMIDIMessage_NoteOn    = 0x9,
   kMIDIMessage_NoteOff   = 0x8,
};



// Play the mid note
- (IBAction) startPlayMidNote:(id)sender {

    UInt32 noteNum = kMidNote;
    UInt32 onVelocity = 127;
    UInt32 noteCommand =  kMIDIMessage_NoteOn << 4 | 0;

      OSStatus result = noErr;
    require_noerr (result = MusicDeviceMIDIEvent(self.samplerUnit, noteCommand, noteNum, onVelocity, 0), logTheError);

logTheError:
   if (result != noErr) NSLog (@"Unable to start playing the mid note. Error code: %d '%.4s'\n", (int) result, (const char *)&result);
}


// Stop the mid note
  - (IBAction) stopPlayMidNote:(id)sender {

    UInt32 noteNum = kMidNote;
    UInt32 noteCommand =    kMIDIMessage_NoteOff << 4 | 0;

    OSStatus result = noErr;
    require_noerr (result = MusicDeviceMIDIEvent(self.samplerUnit, noteCommand, noteNum, 0, 0), logTheError);

 logTheError:
    if (result != noErr) NSLog (@"Unable to stop playing the mid note. Error code: %d '%.4s'\n", (int) result, (const char *)&result);
}


/*!
@function   MusicDeviceMIDIEvent
@abstract   Used to sent MIDI channel messages to an audio unit

@discussion This is the API used to send MIDI channel messages to an audio unit. The status and data parameters 
            are used exactly as described by the MIDI specification, including the combination of channel and 
            command in the status byte.

@param          inUnit
            The audio unit
@param          inStatus
            The MIDI status byte
@param          inData1
            The first MIDI data byte (value is in the range 0 < 128)
@param          inData2
            The second MIDI data byte (value is in the range 0 < 128). If the MIDI status byte only has one 
                data byte, this should be set to zero.
@param          inOffsetSampleFrame
            If you are scheduling the MIDI Event from the audio unit's render thread, then you can supply a 
                sample offset that the audio unit may apply when applying that event in its next audio unit render. 
                This allows you to schedule to the sample, the time when a MIDI command is applied and is particularly 
                important when starting new notes. If you are not scheduling in the audio unit's render thread, 
                then you should set this value to 0

@result         noErr, or an audio unit error code
*/
extern OSStatus
MusicDeviceMIDIEvent(   MusicDeviceComponent    inUnit,
                    UInt32                    inStatus,
                    UInt32                  inData1,
                    UInt32                  inData2,
                    UInt32                  inOffsetSampleFrame)                __OSX_AVAILABLE_STARTING(__MAC_10_0,__IPHONE_5_0);
Run Code Online (Sandbox Code Playgroud)

Bit*_*ank 7

作者以这种方式编写代码的原因最有可能使其更容易理解.MIDI事件代码分为2部分,其中高4位是事件,低4位是通道号.通过向您显示一个恒定左移4位和OR与0,他表示MIDI事件正在使用该常量作为通道0上的事件代码.

  • @BrianRoach这不是OP所说的吗?(0x90 == 144) (2认同)