//装载于http://blog.csdn.net/v_JULY_v/article/details/6347454

/********************************************
此程序通过位数组对字符进行排序的Demo:
********************************************/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define BYTESIZE 8 //定义每个Byte中有8个Bit位
void SetBit(char *p, int posi)
{
 int i;
 for(i = 0; i < (posi / BYTESIZE); i++)
 {
  p++;
 }
 *p = *p | (0x01 << (posi % BYTESIZE));//将该Bit位赋值1
 return ;
}
void BitMapSortDemo()
{
 int i, j;
 int num[] = {3,5,2,10,6,12,8,14,9};
 //bufferLen这个值是根据带排序的数据中最大值确定
 //带排序的最大值是14,因此只需要2个Buytes(16个Bit)就可以了
 const int bufferLen = 2;
 char *pBuffer = (char *)malloc(bufferLen * sizeof(char));
 memset(pBuffer, 0 ,bufferLen);
 //将相应Bit位设置为1
 for(i = 0; i < 9; i++)
 {
  SetBit(pBuffer, num[i]);
 }
 //输出排序结果
 for(i = 0; i < bufferLen; i++)
 {
  for(j = 0; j < BYTESIZE; j++)
  {
   if((*pBuffer&(0x01<<j)) == (0x01<<j))
   {
    printf("%d ", i * BYTESIZE + j);
   }
  }
  pBuffer++;
 }
 printf("\r\n");
 return ;
}
int main()
{
 BitMapSortDemo();
 return 0;
}