Programming on bit level a introduction Print E-mail
Written by Leo, Monday, 05 June 2006

When programming microcontrollers or using the LPT / COM port of a PC you often program on bit level.

Before the real stuff, first a short introduction of conversions of numeral system
 Conversion of numeral system.You properly know how the hex system counts, if not there is enough to find with Google. Below are some examples for conversions. If you have trouble to convert one of them you can also use the windows calculator in scientific mode.To convert 8 bits to hex, first spit them into a nibble = (4 bits).1111 means 0x0F, 0000 = 0x00.Example:Binary to hex1010 0010   A       2          = 0xA2

 
Hex to DecA*16=1602 *1=     2            ----            162A easy reminder for the Dutch people:In Hex  is the value of  ‘D’   Dertien in dec. Let’s move on to set/clear/invert/shift/test  bit’s

Set a bit.
A bit can be set by using the bitwise-or operator. The bit that you want to be set have to be OR with 1, all others with 0; 1010 0010  [data in the variable]0000 1000  [ the bit you want to set; bit3]-------------- [OR]1010 1010Example in CYou want to set the bit 3 but not changing the other bits.So binary value 0000 1000  (=0x08) into a variable ‘porta’porta = porta | 0x08;

It is also possible to write it shorter:

porta |= 0x08;
 Warning! Don’t confuse the bitwise-or  |  with the logical-or ||For instance 0xA2 | 0x08 = AA

0xA2 || 0x08 = 0x01

Clear a bit
A bit can be cleared by using the bitwise-and operator. The bit that you want to be cleared have to be AND with 0, all others with 1;1010 1010  [data in the variable]1111 0111 [ the bit you want to clear; bit3 ]-------------- [AND]1010 0010 Example in CYou want to clear bit 4.porta =  porta & 0xF7;shorter notation:porta & = ~0x08;

 
the last method is more convenient . The ~ means bitwise-not, so 0x08 will be converted to 0xF7.Here you also need to look by using the right operator. The logical-not operator ! will give false as answer. 0x08 is greater then zero, so inverting 0x08 result in false.The logical-and function on bits will always result in true. Both numbers are greater then zero so you get 1 && 1 = 1

 

Invert a bit
A bit can be inverted by using the bitwise-exor. Exor the bit you will change with ‘1’ all other with ‘0’. 1010 10100001 1000 bit mask------------- xor1011 0010

 
Example in Cporta =  porta ^ 0x08or shorter:porta ^ = 0x08;

 

Test bit for 1
A bit can be tested true by isolating  the bit from the others. This can be done by AND all other bits with ‘0’

 
Example in Cif (porta&0x08 == 0x08){//do something}; shorter notation:

if (porta&0x08)

 

Test bit for 0
Same as above. Example in Cif (porta&0x08 == 0x00){//do something};shorter notation:if (!porta&0x08)orif (~porta&0x08)

 Shift bitsShift bitsThere are two operators for shift, the << for shifting to the left and the >> for shifting to the right. Example in Cporta =  porta >> 0x02or shorter:porta >>= 0x02;

this instruction will shift the data 2 positions to the left.For instance, porta contains [0x08]  0000 1000 After the shift instruction0000 0010 [0x02]The content is divided by 2*2Shifting to right is the oppositeporta <<= 0x02;porta now contains 0x08 again, so it’s multiplied by 2*2

Instructions for changing multiply bits keeps the same, however by testing multiply bits the shorted notation can’t be used anymore!

Comments
Add NewSearchRSS
Write comment
Name:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 

Powered by JoomlaCommentCopyright (C) 2006 Frantisek Hliva. All rights reserved.Homepage: http://cavo.co.nr/

 
< Prev   Next >