Embedded programming requires at the very minimum a casual understanding of binary, hexadecimal, and logical operations. Here is a brief overview of these operations and a few sample uses.

Binary

Binary is all computers understand. It is a numbering system quite different than our “base-10″ system. In “base-10″ or decimal numbers, every digit has 10 possible values, 1,2,3,4… (hopefully you know this!). In binary, there are 2, only 2. These are typically represented by 1 and 0, but it can be a bird and a dog, up/down, left/right, front/back, black/white, ANYTHING, as long as it’s only two.

Quick Info on Binary:

a binary digit is known as a ‘bit’

4-bits are called a nibble (or nybble)

8-bits are called a byte

binary numbers are typically grouped into nybbles for readability and ease of conversion to HEX

So here’s a binary number: 0110 1000. An 8-bit number. Now what IS the number? Well it’s 104 obviously!

Here’s how we translate binary to decimal. First we need to disect our 8-bit number:

bit #

7

6

5

4

3

2

1

0

104

0

1

1

0

1

0

0

0

Bit 0 is known as the LEAST SIGNIFICANT BIT, and bit 7, the MOST SIGNIFICANT BIT. Bit numbers are increasing LSB to MSB. You get the value 104 by adding 2^(bit #) of all bits that are 1. So for this number it’s: (2^6 + 2^5 + 2^3) = 64+32+8 = 104.

The maximum value of an X-bit number is (2^X)-1. And the maximum number of different values that can be represented by an X-bit number is 2^X. So an 8-bit number can have 256 different values, and a max value of 255 (ranging 0-255 gives us 256 nums).

I guarantee you there are better tutorials on binary out on the net, search around, I’m going for quick-n-dirty here!

Hexadecimal

Hex is binary’s big brother in a way… It’s on the other side of the spectrum from binary to decimal, in hex, each digit can be 16 different values (0,1..8,9,A,B..E,F). So our 104 from before becomes: 0x68. The notation “0x” is typical to denote a hex number.

Lets try another number in more detail; 190. In binary that is: 1011 1110. So in hex we have 0xBE.

An easy way to do bin->hex is when the binary number is broken into nybbles, you can picture each nybble as 1 hex digit. The max value of a 4-bit number is 15, F in hex, so the conversion is pretty easy!

0110 1010 1101 1111 0010 1011 -> 0x6ADF2B

Logic

AND

0

1

0

0

0

1

0

1

OR

0

1

0

0

1

1

1

1

NOT

0

1

1

0

XOR

0

1

0

0

1

1

1

0

With blue being the input, or numbers being operated on, and green being the result.

AND – The AND (&) op. results in a 1 if and only if both inputs are 1.

OR – The OR (|) op. results in a 1 if either input is 1. (inclusive or)

XOR – The XOR (^) op. results in a 1 if strictly ONE input is 1, and the other is 0.

NOT – The NOT (!) op. flips the bit, if the input is 1, the result is 0. (a.k.a. compliment)

This is BARELY scratching the surface of logic, but it’s enough to get us by. Here are typical uses for these operations:

AND (&)

AND is good for “masking out” bits. If you wanted to get rid of a few bits in a number, while leaving the others unchanged, AND is what you’d use. Example: I have a 8-bit number, but I only want the upper 4-bits. My number is 0101 1010 = 0x9A. To mask these out, I create the “bitmask” 1111 0000 = 0xF0. The 1’s are the bits I want to keep, the 0’s what I want to get rid of.

0x9A & 0xF0 = 0x90 = 0101 0000 (lower bits have been removed)

OR (|)

OR is just the opposite. It is used for “setting” bits, while leaving the rest unchanged. If you have a byte, and need to set a couple bits in that byte, you “OR them in”. Example: I have a byte, and I want to set bits 0,3,6. My number is 1011 0110 = 0xB6. To OR them in, I make a “bitmask” of 0100 1001 = 0x49. The 1’s are the bits I want to SET, the 0’s the bits I want to leave alone.

0xB6 | 0x49 = 0xFF = 1111 1111 (the bits have been set)

XOR (^)

Exclusive or is a toggler, it’s used to flip bits based on their current value. Example: I have a byte, and I need to toggle the upper 4-bits of it. The number is 1100 0101 = 0xC5. To toggle them I make a “bitmask” of 1111 0000 = 0xF0, the 1’s being the bits I want to toggle, the 0’s being the bits I want to keep the same.