1 /*
2  * Copyright (c) 2007 - 2015 Joseph Gaeddert
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 //
24 // script to generate c_ones_mod2 table
25 //
26 
27 #include <stdio.h>
28 
29 // count the number of ones in a byte
30 unsigned int count_ones(unsigned char);
31 
main()32 int main() {
33 
34     unsigned char c_ones_mod2[256];
35 
36     unsigned int b;
37     for (b=0; b<256; b++)
38         c_ones_mod2[b] = count_ones((unsigned char)b) % 2;
39 
40     // print results
41     printf("unsigned char c_ones_mod2[] = {\n    ");
42     for (b=0; b<256; b++) {
43         printf("%1u", c_ones_mod2[b]);
44         if (b==255)
45             printf("\n");
46         else if (((b+1)%16)==0)
47             printf(",\n    ");
48         else
49             printf(", ");
50     }
51     printf("};\n");
52     return 0;
53 }
54 
55 // count the number of ones in a byte
count_ones(unsigned char _b)56 unsigned int count_ones(unsigned char _b)
57 {
58     unsigned int i, n=0;
59     for (i=0; i<8; i++) {
60         n += _b & 0x01;
61         _b >>= 1;
62     }
63     return n;
64 }
65