views:

322

answers:

6

I am currently planning to conduct a Training Workshop on Optimized C Code for Microcontroller Applications.

Currently I am stuck on how to explain bit manipulation in C?

Exactly what I don't want is to lead students to bit structure but guide them to use bit manipulation efficiently...

Any advice?

+4  A: 

I think you should focus on what actual tasks are solved with bit manipulation so that they understand why. Bit manipulation itself is quite simple but can be overwhelming if a person doesn't understand why they are exposed to it in the first place.

sharptooth
A: 

Perhaps start with an inefficiently implemented, but useful, solution.

And then show how an efficient implmentation is done.

djna
+14  A: 

Don't start off with the C statements and syntax. Start them with basic Boolean algebra, and then have them do a bunch of binary arithmetic by hand. Once they actually understand the math behind AND, XOR, NAND, etc, the programming syntax will be immediately understandable in any language. More importantly, they will be entirely familiar with useful transformations like de Morgan's laws, and other properties of bitwise algebra like transitivity that can be used to collapse long ops into shorter ones.

Crashworks
+1  A: 

guide them by giving them examples of different logic gates and laws, you can also give couple of examples of designing logical circuits.

Pranali Desai
+3  A: 
  1. Truth tables of AND, OR, and XOR
  2. Binary representation of decimal and hex numbers
  3. Apply AND, OR, and XOR to binary numbers.
  4. Set a bit without modifying other bits
  5. Clear a bit without modifying other bits
  6. Toggle a bit without modifying other bits
Robert
+2  A: 

Start with the basics and work up.

Basic Boolean Algebra

Practice Boolean Algebra with Truth Tables. Write column of all inputs and break down the steps to calculate.

Binary Logical Connectives

Not

A | Not A
--+-------
0 |   1
1 |   0

And

A | B | A And B
--+---+--------
0 | 0 |    0 
0 | 1 |    0
1 | 0 |    0
1 | 1 |    1

Or

A | B | A Or B
--+---+-------
0 | 0 |    0 
0 | 1 |    1
1 | 0 |    1
1 | 1 |    1

Xor

A | B | A Xor B
--+---+--------
0 | 0 |    0 
0 | 1 |    1
1 | 0 |    1
1 | 1 |    0

An Exercise:

(A And B) Or (B And C)

A | B | C | A And B | B And C | (A And B) Or (B And C)
--+---+---+---------+---------+-----------------------
0 | 0 | 0 |    0    |    0    |           0    
0 | 0 | 1 |    0    |    0    |           0    
0 | 1 | 0 |    0    |    0    |           0    
0 | 1 | 1 |    0    |    1    |           1    
1 | 0 | 0 |    0    |    0    |           0    
1 | 0 | 1 |    0    |    0    |           0    
1 | 1 | 0 |    1    |    0    |           1        
1 | 1 | 1 |    1    |    1    |           1

Binary Representations

Hexadecimal Representation

Hex | Binary
----+-------
0   |  0000
1   |  0001
2   |  0010
3   |  0011
4   |  0100
5   |  0101
6   |  0110
7   |  0111
8   |  1000
9   |  1001
A   |  1010
B   |  1011
C   |  1100
D   |  1101
E   |  1110
F   |  1111

So,
1A6 = 0001 1010 0110

Logical Statement Reduction

Properties of Boolean Algebra

De Morgan's Laws

Not (A Or B) = (Not A) And (Not B)
Not (A And B) = (Not A) Or (Not B)

Examples of and code for Bit Manipulation Uses

There is a very good article on many uses for Bit Manipulation Uses called Bit Twiddling Hacks by Sean Eron Anderson.

lillq