Friday, December 25, 2009

What is Masking in programming microcontrollers?

Please answer in context of MikroC.What is Masking in programming microcontrollers?
A bit mask is a binary number that is combined with some value stored in a variable or memory location through a bit-wise operation.





For example perhaps one has a number stored in a 8 bit memory location but only 3 of the bits are valid for some purpose. The programmer may want to set the other five bits to zero without changing the three bits of interest. If the bits of interest are the least significant three bits the following mask could be applied by a bit-wise AND:


00000111 (the mask)


Any place there is a 1 in the mask the bits will be unchanged. Any place there is a zero the bit will be cleared.


00000111 %26amp; 10111010 =%26gt; 00000010





A bit mask can also be used to set selected bits while leaving other bits unchanged.


00000111 | 10111010 =%26gt; 10111111





A bit mask can also be used to toggle bits. Using bit mask with an exclusive or results in toggling the bits in locations the mask has a zero and not changing the bits where a zero occurs.


00000111 ^ 10111010 =%26gt; 10111101





Often bit masking is the way to go when writing programs in assembly language. In C an alternative to using bit masks exists in many applications. In C one can define a bitfield. Making use of a bitfield often results in code that is easier to read and understand than using bit masks.What is Masking in programming microcontrollers?
Masking, generally, refers to performing a bit-wise logical operation, such as AND, OR, XOR, NAND etc on a data word (or literal) with another data word (or literal), the bit-wise result being the result of the logical operation applied to each positional bit of each argument as argument to the corresponding logical function.





You shouldn't be programming micro controllers yet if you don't even understand that, it's a fundamental thing that is often needed to control output pins and to perform calculations efficiently.

No comments:

Post a Comment