Bitwise operators in C can be very handy in writing simple and efficient codes to solve many logical requirements.
Just to get an overview of how the bitwise operators in C can solve logical problems easily, here we are giving two examples which are tested in Linux environment.

1. Find whether the given input number in even or odd.
2. Find the missing number from the series of natural numbers starting from 1.

Note: To make the code easily understandable, some of the common defensive programming techniques are omitted.
.
.
1. Find whether the given input number is even or odd.

Here in this simple program, the input is read from the ‘stdin’ and read into the variable ‘input’. If the entered value is zero, we are jumping to ‘end’ and exits the program.

The code, res = !(input & 1); is computing whether the given number is even or odd. Here the variable ‘res’ will be set to ‘TRUE’ if the variable ‘input’ is containing an even number. Since the last bit of even number will be zero, the ANDing of ‘input’ with 1 will result in zero ((input & 1)), thereby making the entire right hand side operation to ‘TRUE’ (!(input & 1))

2. Find the missing number from the series of natural numbers starting from 1.

In this program we can find the missing number from the given list of natural numbers starting from 1. The input list of numbers is stored in the array variable ‘real_input’. The function ‘GetTheOneMissingNumber()’ will find out the missing number from the given list.

In this program we are performing XOR between all the real numbers starting from 1 to ‘count+1’ and saving it to the local variable ‘xor_res2’. Similarly all the elements in the given list ‘input_list’ are also XOR’ed and saved into the local variable ‘xor_res1’. A final XOR between the values of ‘xor_res2’ and ‘xor_res1’ will give the missing number.

Computation explained,
xor_res1 = X1 ^ X2 ^ X3 ^ X5 ^ X6 ^ X7 ^ X8 ^ X9 ^ X10
xor_res2 = X1 ^ X2 ^ X3 ^ X4 ^ X5 ^ X6 ^ X7 ^ X8 ^ X9 ^ X10
xor_res1 ^ xor_res2 = (X1 ^ X1) ^ (X2 ^ X2) ^ (X3 ^ X3) ^ (X4) ^ (X5 ^ X5) ^… (X10 ^ X10) = X4

### PIC16F886 Assembly example code for UART echo test using interrupt

This example assembly code for PIC16F886 can be used to learn the working of the UART module within the PIC MCU. The assembly code project is comprised of two *.asm files and can be compiled using MPLAB IDE.

Working:
The program will echo back the characters which were entered from a PC (Personal Computer).
.
.
.
.
.
.
.
Source files:

1. main.asm

2. uart.asm

PS: The above assembly program is having a special behavior while executing the same. Readers are encouraged to find out the program behavior and are welcome to comment on this. 🙂

### LED Blink Assembly code for PIC16F886 using Timer0 interrupt

The below is a working example of LED blink program using PIC assembly code which can be compiled on MPLAB IDE.

Abstract:

The LED is connected to RC4 of PIC16F886 and the circuit is setup as active LOW. Here the LED will glow when the RC4 output is made to ‘0’.
.
.
.
.
.
.
.
The PIC configurations used are,

There are 3 global variables also used for the project which are shown as below.

There are 2 labels also used as shown below,

Complete Assembly program: