Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C

Bitwise Operation Explained

Rate me:
Please Sign up or sign in to vote.
4.76/5 (117 votes)
19 Aug 2008CPOL6 min read 82.4K   392   98   17
Few well known bitwise operation problem collection

Background

After reading the wonderful article, “An introduction to bitwise operators” by PJ Arends, I thought of extending the scenario further and thought of putting few common uses of bitwise operations that I have faced in real life. The above mentioned article is indeed a pre-requisite to this one.

Bitwise operations are really a joy to use. If used with care, they give a tremendous good look to the code and performance too.

Using the Code

So, let’s start with a vintage problem introduced by Denis Ritchie, “You have a variable (say int) at your disposal, you need to find out how many bits are set (i.e. 1) in the bit pattern beneath”.

Say, you have declared an int var = 13, then the bitwise structure of var will be:

512 256 128 64 32 16 8 4 2 1
0 0 0 0 0 0 1 1 0 1

Shaded cells indicate the power of each bucket, in the higher order, all buckets actually exist, but we ignored them for this example. Now let’s write a method that will return the number of bit sets in this variable:

C++
int __numOf_SET_Bits(int var){
 if (var==0) return 0;
 else return (var&01)?1+__numOf_SET_Bits(var>>1):__numOf_SET_Bits(var>>1);
}

A simple recursive function that counts the total number of set bits in the bit pattern for a variable. In the heart of the function, there exist two bitwise operations:

  1. One which checks whether the current right most bit has a numeric value of 1 or not (checks by a bitwise AND with a 1), if so increment the total count by one.
  2. While calling the function recursively, make a right shift by 1 over the variable, thus truncating the right most bit of the variable (and left most bit will be padded by a zero).

There also exists an iterative version of this algorithm, please refer “C Programming Language” book by Denis Ritchie.

Ok, now let’s analyze another similar kind of problem, “Find out whether a number is even or odd”. The naïve algorithm is:

C++
#define isEven(a) \
  ((((a)%2)==0)?1:0)

But if you closely look at the bit pattern for any variable, you can see all the buckets in the bit pattern have a power of even number except the right most one (which has a power of 1). Now since (Even Number + Even Number) = Even Number and (Even Number + Odd Number) = Odd Number, we can conclude for a even number the right most bit must have a value ZERO, and for an odd number the right most bit must be set to ONE. So let’s try to write a function/macro considering this property:

C++
#include <stdio.h>
#define isEven(a) \
  ((((a)&01)==0)?1:0)
int main()
{
 int var=1;
 if(isEven(var)){
    printf("%d is a even number \n",var);
 }
 else
    printf("%d is a odd number \n",var);
 return 0;
}

Wow, this one looks cool.

Now let's analyze another problem, “Find out whether a number has a form of power of 2 or not”, i.e., all the numbers 1,2,4,8,16,32,64.128……. have a form of power of 2. How to find this out? I am going to give two solutions.

If you look closely at the bit pattern, all the numbers have a form of power of 2, for all of them exactly one bit is set, rest all are zero in the bit pattern. Now if we bitwise-AND-ed that number with a number having a value less than 1, the operational outcomes will be zero. Consider the example of 128:

Number 128 64 32 16 8 4 2 1
128 1 0 0 0 0 0 0 0
128-1=127 0 1 1 1 1 1 1 1
128 bitwise-AND 127 0 0 0 0 0 0 0 0

Voila, we can use this property indeed. Let's write our first version of the code:

C++
#include <stdio.h>
#define __isPower_of_TWO(a) \
 (((a)&(a-1))==0)?1:0
int main(){
 int arr[] = {1,2,3,4,5,8,9,16};
 int i=0;
 for(;i<sizeof(arr)/sizeof(arr[0]);i++){
        if (__isPower_of_TWO(*(arr+i)))
           printf("%d has a form of Power of Two \n",*(arr+i));
        else
           printf("%d is not in the form \n", *(arr+i));
 }
 return 0;
}

Now consider another way to do this:

C++
#include <stdio.h>
#define __isPower_of_TWO(a) \
 (((a)&(-a))==a)?1:0
int main(){
 int arr[] = {1,2,3,4,5,8,9,16};
 int i=0;
 for(;i<sizeof(arr)/sizeof(arr[0]);i++){
        if (__isPower_of_TWO(*(arr+i)))
           printf("%d has a form of Power of Two \n",*(arr+i));
        else
           printf("%d is not in the form \n", *(arr+i));
 }
 return 0;
}

Really it’s another nice way to do the same job.

Now consider another famous problem of swapping the value of two variables without using the third one. The naïve algorithm uses the arithmetic operator to do so. The smarter way to do it is to use Bitwise-XOR operation, the XOR operation has a unique property: returns 0 when the bits have the same value (both zero, or both one) and returns 1 when their values are different (one is zero and the other is one). Let’s first write the logic, and then we will see it with an example.

C++
#include <stdio.h>
void __SWAP(int *a,int *b){
 *a = *a ^ *b;
 *b = *a ^ *b;
 *a = *a ^ *b;
}
int main()
{
 int a=5, b=6;
 printf("Before swap: a=%d <=====> b=%d \n",a,b);
 __SWAP(&a,&b);
 printf("After  swap: a=%d <=====> b=%d \n",a,b);
 return 0;
}

In the core of the logic, there are three sequential bitwise-XOR operations, which swap the value in-turn. Awesome, let's see an example.

Initially bit-pattern of a and b is:

a 0 0 0 0 0 1 0 1
b 0 0 0 0 0 1 1 0

Now after first bitwise-XOR operation, value of “a” and “b” become: (check only the bit-pattern of “a” will change, “b” will remain the same for this step. (* indicates which bit has changed the state).

a 0 0 0 0 0 0 (*) 1 (*) 1
b 0 0 0 0 0 1 1 0

After the second operation: (here only “b” will be changed, and check bit pattern of “b” now contains original bit-pattern of a, so the value of “a” has been shifted over b).

a 0 0 0 0 0 0 1 1
b 0 0 0 0 0 1 0 (*) 1 (*)

After the third operation: (here only value of “a” will be changed, and voila! Look the value of “b” has been shifted over “a”, swap operation got passed).

a 0 0 0 0 0 1 (*) 1 0 (*)
b 0 0 0 0 0 1 0 1

Now, let’s analyze a data structure building technique, named “XOR linked list”. A doubly linked list is a list for which each node contains a data member and two pointers pointing to the previous and next item in the list. For the last node in the list the value of the “next” is NULL, for the first node (head) the value of “prev” is NULL. If a head itself contains a NULL, then the list is empty. Since each node contains two address parts (one for predecessor and another for successor) the storage requirement gets higher. A bitwise-XOR operation can compress the same information into one address field.

Consider the following code:

C++
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct XOR_based_Node {
 int data;
 unsigned long compressedAddress;
}node;
node* head = NULL;
void add_element_to_list(node** headRef, int data)
{
  node *newNode = (node*)malloc(sizeof(node));
 assert(newNode);
 newNode->compressedAddress = (unsigned long)(*headRef);
 newNode->data = data;
 if(*headRef != NULL)
  (*headRef)->compressedAddress ^= (unsigned long)newNode;
 *headRef=newNode;
}
void printList(node* head)
{
  unsigned long prev = 0;
 while(head) {
  unsigned long next = prev ^ head->compressedAddress;
  printf("%d ", head->data);
  prev = (unsigned long)head;
  head = (node *)next;
 }
 printf("\n");
}
int main(void) {
 int i=0;
 for(;i<10;i++)
   add_element_to_list(&head,i);
 printList(head);
 return 0;
}

In this code, all the address calculations have been done in a single address field (that we have per node). This field has been calculated while we try to push a node in the list using bitwise-XOR operation and the proper memory location has been fetched using bitwise-XOR operation too.

Given below is the output:

bash-3.2$ gcc -g -o hello XoR_List.c 
bash-3.2$ ./hello
9 8 7 6 5 4 3 2 1 0 
bash-3.2$

Conclusion

There are tons and tons of real life examples of using bitwise operations. In the second part of the article, I'll focus on the data structure only (heap, priority queue, red-black tree, splay tree) and will try to explain how a bitwise operation can help us greatly to implement those.

References

  1. "C Programming Language" by Denis Ritchie

History

  • 19th August, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Rebaca Technologies
India India
int main(){
while(!isSleeping())
{
write_code();
}
return 0;
}

Comments and Discussions

 
QuestionNice Article Thank You Pin
Member 118470888-Jan-19 23:14
Member 118470888-Jan-19 23:14 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey28-Feb-12 17:55
professionalManoj Kumar Choubey28-Feb-12 17:55 
GeneralIsEven.. fora double Pin
CanopenR30-Mar-09 10:16
CanopenR30-Mar-09 10:16 
Generalswap [modified] Pin
J0ker12-Sep-08 17:39
J0ker12-Sep-08 17:39 
GeneralRe: swap Pin
programmersmind15-Sep-08 5:57
programmersmind15-Sep-08 5:57 
GeneralRe: swap Pin
Bartosz Wójcik23-Sep-08 8:19
Bartosz Wójcik23-Sep-08 8:19 
RantRe: swap - undefined behavior Pin
J0ker24-Sep-08 5:07
J0ker24-Sep-08 5:07 
GeneralBeware of signed numbers Pin
billyvas25-Aug-08 19:38
billyvas25-Aug-08 19:38 
GeneralRe: Beware of signed numbers Pin
programmersmind3-Sep-08 11:28
programmersmind3-Sep-08 11:28 
GeneralOther Operations Pin
Hawk77725-Aug-08 14:47
Hawk77725-Aug-08 14:47 
There are other, more advanced things that can be done with bitwise operations than are listed here.

For one thing, the algorithm to count set bits presented in this article is not particularly efficient. A more efficient solution is this:

unsigned int count_bits(unsigned int value) {
  value = (value & 0x55555555U) + ((value >> 1 ) & 0x55555555U);
  value = (value & 0x33333333U) + ((value >> 2 ) & 0x33333333U);
  value = (value & 0x0F0F0F0FU) + ((value >> 4 ) & 0x0F0F0F0FU);
  value = (value & 0x00FF00FFU) + ((value >> 8 ) & 0x00FF00FFU);
  value = (value & 0x0000FFFFU) + ((value >> 16) & 0x0000FFFFU);
  return value;
}


which takes only ten ANDs, five shifts, and five adds for a total of twenty operations, whereas the provided example goes to a recursive depth of 32 and does one AND, one (potentially expensive!) conditional, one shift, and (maybe) one add for each level, for a total of between 96 and 128 operations.

The algorithm above works by dealing with multiple blocks of bits in parallel. Rather than considering "value" as just a pile of bits, consider it to be a list of buckets each of which contains a counter indicating the number of bits set within a particular subrange of the original value. Initially each bit is its own bucket: given a single bit, obviously, if the bit is zero, then zero bits are set within that range, and if the bit is one, then one bit is set within that range. Each line of the above algorithm takes every pair of adjacent buckets and replaces them with a single larger bucket containing the sum of the original two buckets. For example, the first line takes the original value (with 32 1-bit buckets) and takes every adjacent pair of bits, adds them together (thus giving a value 0, 1, or 2), and stores the result into a 2-bit-long bucket in the space where the original two bits were. The second line similarly turns the 16 2-bit buckets into 8 4-bit buckets, and so on. The last line ends up giving you a single 32-bit bucket, which is the number of bits set in the original value.



Another interesting thing you can do with bitwise operations is set-related stuff. Assuming you represent a set as an integer (where a 1 bit means the corresponding element is present and a 0 bit means it's absent), you can take the union of two sets with OR and the intersection with AND. You can also find all possible subsets of a set like so:

for (unsigned int subset = set; subset; subset = (subset - 1) & set) {
  // Use this subset.
}


noting that this will *include* the original set but *exclude* the empty set. The key to understanding this algorithm is to realize that "subset - 1", due to the way borrowing works in binary subtraction, will *remove* the smallest element that's present, while *inserting* every element below it. ANDing with "set" then limits "every element below it" to be only those present in the original set.
GeneralRe: Other Operations Pin
programmersmind25-Aug-08 15:10
programmersmind25-Aug-08 15:10 
GeneralRe: Other Operations Pin
wtwhite25-Aug-08 15:57
wtwhite25-Aug-08 15:57 
GeneralRe: Other Operations Pin
Hawk77725-Aug-08 20:15
Hawk77725-Aug-08 20:15 
General"Find out whether a number has a form of power of 2 or not" Pin
phinecos21-Aug-08 16:03
phinecos21-Aug-08 16:03 
GeneralRe: "Find out whether a number has a form of power of 2 or not" Pin
peterchen25-Oct-08 11:25
peterchen25-Oct-08 11:25 
GeneralComment Pin
User 5924119-Aug-08 14:11
User 5924119-Aug-08 14:11 
GeneralRe: Comment Pin
programmersmind19-Aug-08 14:15
programmersmind19-Aug-08 14:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.