Hardware Reference
In-Depth Information
Hex: What Is It Good For?
Since you can manipulate binary protocols bit by bit,
you're probably wondering what hexadecimal notation
is good for. Hex is useful when you're working in groups
of 16, of course. For example, MIDI is grouped into banks
of instruments, with 128 instruments per bank, and each
instrument can play on up to 16 channels. So, those
command bytes you were manipulating earlier could also
be manipulated in hex. For example. 0x9 n is a Note On
command, where n is the channel number, from 0 to A in
hex. 0x9A means note on, channel A (or 10 in decimal).
Similarly, 0x8A means note off, channel A. Once you
know that MIDI is organized in groups of 16, it makes a
lot of sense to read and manipulate it in hexadecimal. In
fact, many binary protocols can be grouped similarly, so
knowing how to manipulate them in binary or hexadecimal
is handy.
XOR (^) : if the bits are not equal, the result is 1. Otherwise,
it's 0:
1 ^ 1 = 0
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
Using the logical, or bitwise , operators, you can isolate one
bit of a byte, like so:
// Check if bit 7 of someByte is 1:
if (someByte & 0b10000000) {}
In MIDI command bytes, for example, the command is the
leftmost four bits, and the MIDI channel is the rightmost
four bits. So, you could isolate the MIDI channel by using
the AND operator:
The next project puts these principles in action to control a
MIDI synthesizer.
X
channel = commandByte & 0b00001111;
Or, you could use bit shifting to get the command and lose
the channel:
command = commandByte >> 4;
Combined, these bit-manipulation commands give you all
the power you need to work with binary protocols.
 
Search WWH ::




Custom Search