Search This Blog

Wednesday, December 3, 2014

Program for putting bits into buffer. This is useful in compression of data, avoide wastage of bits. This program will work for any 32-bits integers.

1:  /******************************************************************************  
2:   *         Include Files  
3:   * ***************************************************************************/  
4:  #include <stdio.h>  
5:  #include <stdlib.h>  
6:  #include <string.h>  
7:  /******************************************************************************  
8:   *         Macros  
9:   * ***************************************************************************/  
10:  #define UINT_64 unsigned long long int  
11:  #define UCHAR unsigned char  
12:  #define OCTET 8  
13:  /******************************************************************************  
14:   *         Global Variables  
15:   * ***************************************************************************/  
16:  /*  
17:   * Remaining Bits give information about empty bits left in one octet  
18:   */  
19:  static int remaining_bits = 8;  
20:  /*  
21:   * Global Index for Octet shifting  
22:   */  
23:  static int idx;  
24:  /*  
25:   * Offset for last set bit from LSB  
26:   */  
27:  static int offset = 0;  
28:  /******************************************************************************  
29:   * This function used to calculate offset length for given Decimal number  
30:   ******************************************************************************/  
31:  int find_offset(UINT_64 num)  
32:  {  
33:    int offset = 0;  
34:    UINT_64 mask = 1<<31;  
35:    while(mask)  
36:    {  
37:      if (num & mask)  
38:      {  
39:        return (32-offset);  
40:      }  
41:      offset++;  
42:      mask >>= 1;  
43:    }  
44:    return 0;  
45:  }  
46:  /*******************************************************************************  
47:   * This function used to fill bits into 2MB buffer  
48:   * ****************************************************************************/  
49:  void put_bits(UCHAR *buf, UINT_64 num)  
50:  {  
51:    int temp = 0;  
52:    /*  
53:     * Checking For OFFSET length  
54:     */  
55:    if ( offset < OCTET )  
56:    {  
57:      /*  
58:       * OFFSET Less than an OCTET means bit patter will fit into single index  
59:       */  
60:      if ( offset < remaining_bits )  
61:      {  
62:        remaining_bits -= offset;  
63:        *(buf + idx) |= num << remaining_bits;  
64:      } else {  
65:        /*  
66:         * This condition executes when bit patter is less than one octet  
67:         * but it entire octet is not available  
68:         */  
69:        *(buf + idx) |= num >> (offset - remaining_bits);  
70:        idx++;  
71:        temp = offset - remaining_bits;  
72:        *(buf + idx) |= num << (OCTET - temp);  
73:        remaining_bits = OCTET - temp;  
74:      }  
75:      return;  
76:    } else {  
77:      /*  
78:       * This condition executes when offset is larger than one octet  
79:       */  
80:      if ( remaining_bits < OCTET)  
81:      {  
82:        /*  
83:         * If last octet is not filled completely, this condition will   
84:         * execute  
85:         */  
86:        offset -= remaining_bits;  
87:        *(buf + idx) |= num >> offset;  
88:        idx++;  
89:        remaining_bits = OCTET;  
90:        if ( offset > 0)  
91:        {  
92:          /*  
93:           * If still offset is there, then start filling form begining  
94:           */  
95:          put_bits(buf,num);  
96:        }  
97:      } else {  
98:        /*  
99:         * This condition will execute when offset is larger than one octet  
100:         */  
101:        *(buf + idx) |= num >> (offset - OCTET);  
102:        idx++;  
103:        offset -= OCTET;  
104:        /*  
105:         * Continue with patter filling process if still offset is still  
106:         * there  
107:         */  
108:        put_bits(buf,num);  
109:      }  
110:    }  
111:  }  
112:  /******************************************************************************  
113:   * Main function  
114:   * ****************************************************************************/  
115:  main()  
116:  {  
117:    int i=0;  
118:    UINT_64 num = 0;  
119:    UCHAR *buff = NULL;  
120:    buff = (char *)malloc(2*1024*1024*sizeof(char));  
121:    if ( NULL == buff)  
122:    {  
123:      printf("failed to allocate memory\n");  
124:      exit(1);  
125:    }  
126:    memset(buff,0,2*1024*1024*sizeof(char));  
127:    do {  
128:      printf("\nEnter any Number or Zero to exit:");  
129:      scanf("%lld",&num);  
130:      /*  
131:       * Finding Last Enabled bit position from LSB  
132:       */  
133:      offset = find_offset(num);  
134:      /*  
135:       * Putting Bit patter into Buffer  
136:       */  
137:      put_bits(buff,num);  
138:      /*  
139:       * Display buffer status  
140:       */  
141:      for (i = 0; i <= idx; i++)  
142:      {  
143:        printf("%4X", buff[i]);  
144:      }  
145:    }while(num != 0);  
146:    printf("\n");  
147:  }  
148:  /******************************* End of File *********************************/