Basics of Embedded Programming

Introduction: Basics of Embedded Programming


Bitwise or bitlevel operations form the basis of embedded programming. A knowledge of Hexadecimal and Binary Numbering system is required along with conversion from binary to hex and vice-verse. The link provides information regarding Hexadecimal Numbers.

Step 1: Types of Bitwise Operator

Bit-wise Operators : Now getting armed with the knowledge of interconversion between
Hexadecimal and Binary we can start with Bitwise(or bit level) operations in C. There are bascially 6 types of Bitwise operators. These are :

1.Bitwise OR operatordenoted by ‘|‘

2. Bitwise AND operator denoted by ‘&‘

3. Bitwise Complement or Negation Operator denoted by ‘~‘

4. Bitwise Right Shift & Left Shift Operator denoted by ‘>>‘ and ‘<<‘ respectively

5. Bitwise XOR operator denoted by ‘^

We'll a take a look at each of these operators in next steps.

Step 2: BitWise OR (|)

Bitwise OR ( | ): This is same as OR Operator in digital logic. If we operate OR 0 with any Number 'N' then result will be the Number i.e. N. itself and if we operate OR 1 with any No Number then result will be 1.

For Eg. if the number is N, then N | 1 = 1 and N | 0 = N with value – 1 | 8 = 9 and 0 | 8 = 8.

Usage in MicroControllers

If you are using PORTA and want to Set pin 17 without changing rest of bits than by using OR Operator:

PORTA |= (1<<17) you can change that bit.

Step 3: Bitwise AND (&)

Bitwise AND ( & ) : Bitwise AND operator is denoted by ‘&’. When 2 numbers are operated as AND then each pair of ‘corresponding’ bits in both numbers are operated as AND. Consider two 4-bit binary numbers ANDed : 1010 & 1101 , here Nth bit of both numbers are operated as AND to get the result. If A and B are two numbers AND with each other ,then if any bit value of A or B is 0 than resultant bit value is zero.

For Example- -(0b10001000) & (0b01111000) = 0b00001000 .

Usage in Microcontroller

If you are using PORTA and want to Clear pin 17 without changing rest of bits than by using AND Operator: PORTA &= ~ (1<<17) you can change that bit.

Step 4: Bitwise Negation ( ~)

Bitwise Negation ( ~) : If we need to convert all 1 in a bit to 0 and all 0 to 1 than that will be done by using operator ~ .

If 1001 is operated with ~ than the result will be 0110. in Hex Number.

For Example- ~(0x0F0F00) ,then result is 0xF0F0FF.

Usage in MicroController

If you are using PORTA and want to toggle all bits of a port than PORTA = ~ PORTA will be use.

Step 5: Bitwise XOR Operator(^)

XOR Operator( ^ ): XOR is short for eXclusive-OR. By definition of XOR , the result will be a ’1′ if both the input bits are different and result will be ’0′ if both are same . XOR can be used to check the bit difference between 2 numbers. if XOR is operated between two numbers ,say A and B and if bit value of A and B are same than result will be 0 and if they are different than result will be 1.

For Eg-(0b01101001) ^ ( 0b00001111) = 0b10010110.

Usage in MicroController

Setting a bit-

Use the bitwise OR operator (|) to set a bit.

ForEg- Number |= 1 << x;That will set bit x.
If we need to store the status of 4 Bits of a PORT we can use

1) x = PORTA ^ 0xF0; // For 4 MSB Bit

2) x = PORTA ^ 0x0F; // For 4 LSB Bit

Step 6: Clearing/Toggling /Checking a Bit

Clearing a Bit:- Use the Bitwise AND operator (&) to clear a bit.

For Eg- number &= ~(1 << x).That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.

Usage in MicroController


Toggling a Bit:The XOR operator (^) can be used to toggle a bit.

For Eg-number ^= 1 << x. This will toggle bit x.

Usage in MicroController


Checking a bit:

To check a bit, AND it with the bit you want to check

For Eg :bit = number & (1 << x);

Usage in MicroController


For more tutorials and for Purchasing development Boards/Sensors/IOTs/DIY Kit please visit:

For Exclusive Tutorials

Be the First to Share


    • Mason Jar Speed Challenge

      Mason Jar Speed Challenge
    • Bikes Challenge

      Bikes Challenge
    • Remix Contest

      Remix Contest



    4 years ago

    Very useful tutorial. Since you only put very limited commercial info I feel this is in keeping with the intent of Instructables.