Bitwise operation using bitwise operators

In this tutorial we will explore the power of bitwise operators in C/C++ programming with examples. Have you ever thought how your email passwords are made secure? Well its done using encryption technique using one of the most advanced and non-reversible encryption algorithm, Md5 encryption. And yes they use bitwise operators. Bitwise operators are infact the most interesting part of programming and coders just love to play with bitwise operators. Many complicated programming algos like cryptography algos make use of bitwise operation.

A bitwise operator operates on one or two bit patterns at the level of their individual binary digits. On old computer architectures bitwise operation was faster than normal arithmetic operation but now with dual core it's almost the same. The real advantage of learning bitwise operation in C/C++ or any other programming language is that we are able to finish our code in few lines and quite often bitwise operation helps us write our code with a better logic. Using bitwise operation we can perform various arithmetic and logic opertaions.

Let us see how can we implement bitwise operation in C/C++. We will deal with OR, XOR, AND, NOT, arithmetic right and left shift operators.Let's take them one by one with examples.

Boolean Operation: (use the windows Calculator in binary mode to practice examples given below )

Boolean OR : We use "|" (single pipe)

The bitwise OR. It takes two binary values and completes OR operation bit by bit. Generally used to set flags where earlier values which are high are preserved and new value is added to the flag.

eg.

OR 1100  (decimal 12)(hexadecimal C)

     1010  (decimal 10)(hexadecimal A)

   =1110  (decimal 14)(hexadecimal E)

*Do not confuse this operator OR "|" with Logical OR "||"

code:

#include <iostream>

using namespace std;

int main() 

{

   unsigned short a = 0xC;    

   unsigned short b = 0xA;

   cout  << hex << ( a | b ) << endl; //prints "e" hex value of c after bitwise OR

}

Boolean XOR : We use "^" (caret)

The bitwise XOR. We often XOR a binary value with it self to clear its value to zero and its also used to encrypt-decrypt values.

eg.

XOR 1010  (decimal 10)(hexadecimal A)

       1010  (decimal 10)(hexadecimal A)

     =0000  (decimal 0)(hexadecimal 0)

Code:

#include <iostream>

using namespace std;

int main() 

{

   unsigned short a = 0xA;    

   unsigned short b = 0xA;

   cout  << hex << ( a ^ b ) << endl; //prints "0" hex value after bitwise XOR

}

Boolean AND : We use "&" (ampersand)

The bitwise AND. It takes two binary values and completes AND operation bit by bit. It may be used for bit mask operation.

eg.

AND 1011  (decimal 11)(hexadecimal B)

       1010  (decimal 10)(hexadecimal A)

     =1010  (decimal 10)(hexadecimal A)

*Do not confuse this operator AND "&" with Logical AND "&&"

Code:

#include <iostream>

using namespace std;

int main() 

{

   unsigned short a = 0xB;    

   unsigned short b = 0xA;

   cout  << hex << ( a & b ) << endl; //prints "A" hex value after bitwise AND

}

Boolean NOT :We use "~" (tilde)

The bitwise NOT or complement.

eg.

NOT 0101  (decimal 5)(hexadecimal 5)

    = 1010  (decimal 10)(hexadecimal A)

*Do not confuse this operator NOT "~" with Logical not "!"

syntax:

b=~b; //It complements the value of b, but values may differ depending on type of architecture, syntax is for example.

Bitwise shift Operation

Arithmetic shift: ">>" (Right-shift) "<<" (Left-shift)

In arithmetic shift of a binary number each bit is shifted to right or left and either of the extreme right or left bit is discarded and a zero is inserted in the vacant bit from left or right respectively. Let's see how.

LEFT-SHIFT    00100010 (decimal 34)(hexadecimal 22)

          =  01000100 (decimal 68)(hexadecimal 44)

*we can observe that the value is multiplied by two

RIGHT-SHIFT   00100010 (decimal 34)(hexadecimal 22)

                   =  00010001 (decimal 17)(hexadecimal 11)

*we can observe that the value is divided by two 

Code:

#include <iostream>

using namespace std;

int main() 

{

   unsigned short a = 0x22;    

   cout  << hex << ( a=a<<1 ) << endl; //prints "44" hex value after   bitwise left-shift   

   cout  << hex << ( a=a>>1 ) << endl; //prints "11" hex value after bitwise right-shift

}

Application: Read the example on encryption and decryption using "exclusive OR" - XOR operator for better understanding.

As told earlier there are a lot of cryptography algos which make use of bitwise operators including Md5 key generation algo. Follow the demo link below to see the inspirational cryptograpy example [online]. Hope you liked this tutorial and please do not hesitate to post your doubts in bOtskOOl forum.