Qualia  0.2
bitarray.h
Go to the documentation of this file.
1 /*
2  * bitarray.h
3  *
4  * (c) 2012 Sofian Audry -- info(@)sofianaudry(.)com
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef BITARRAY_H_
22 #define BITARRAY_H_
23 
24 #include <qualia/core/common.h>
25 #include <string.h>
26 
27 // Writes bit (0 or 1) to destination address #dst# at bit position #pos#.
28 void arrayBitWrite(uint8_t* dst, int pos, uint8_t bitValue);
29 
30 // Sets bit (write 1) at position #pos# of destination address #dst#.
31 void arrayBitSet(uint8_t* dst, int pos);
32 
33 // Clears bit (write 0) at position #pos# of destination address #dst#.
34 void arrayBitClear(uint8_t* dst, int pos);
35 
36 // Flips bit at position #pos# of destination address #dst#.
37 void arrayBitFlip(uint8_t* dst, int pos);
38 
39 // Reads bit at position #pos# of destination address #src#.
40 uint8_t arrayBitRead(const uint8_t* src, int pos);
41 
42 // Writes #length# bits from source #src# to destination #dst#, starting from bit #srcPos#
43 // in source to bit #dstPos# in destination.
44 void arrayBlockWrite(void* dst, const void* src, int dstPos, int srcPos, int length);
45 
46 // Copies #length# bits from source #src# to destination #dst#, starting from bit #pos#
47 // in source. Trailing bits in #dst# are set to zero (0).
48 void arrayBlockCopy(void* dst, const void* src, int pos, int length, int dstByteSize);
49 
50 #define BITARRAY_BIT_TO_BYTE(bit) ((bit) >> 3) // = bit / 8
51 #define BITARRAY_BIT_IN_BYTE(bit) ((bit) & 7) // = bit % 8
52 
53 // Sets array pointer and position properly to call a bit operation macro on them.
54 void initPointerAndPositionForBitOperation(uint8_t** array, int* pos);
55 
56 // Version that works by "blocks".
57 //void copyBits(uint8_t* dst, const uint8_t* src, int pos, int length) {
58 // uint8_t shift = pos % 8;
59 // uint8_t shiftComp = 8-shift;
60 // dst += pos / 8;
61 // //src += pos / 8;
62 // while (length > 0) {
63 // *dst = *src >> shift; // 00000111
64 // if (length >=8)
65 // *dst |= *(src+1) << shiftComp; // len-=: len>=0: 8 - shift
66 // else
67 // *dst &= (0xff >> (8-length));
68 // length -=8;
69 // src++;
70 // dst++;
71 // }
72 //}
73 
74 
75 
77 //macros.h:
78 //Binary constant generator macro
79 //By Tom Torfs - donated to the public domain
80 //*/
81 //
83 //
85 //
87 //(avoids problems with leading zeroes)
88 //8-bit constants max value 0x11111111, always fits in unsigned long
89 //*/
90 //#define HEX__(n) 0x##n##LU
91 //
93 /*#define B8__(x) ((x&0x0000000FLU)?1:0) \
94 +((x&0x000000F0LU)?2:0) \
95 +((x&0x00000F00LU)?4:0) \
96 +((x&0x0000F000LU)?8:0) \
97 +((x&0x000F0000LU)?16:0) \
98 +((x&0x00F00000LU)?32:0) \
99 +((x&0x0F000000LU)?64:0) \
100 +((x&0xF0000000LU)?128:0)
101 */
102 //
104 //
106 //#define B8(d) ((unsigned char)B8__(HEX__(d)))
107 //
109 /*#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<< 8) \
110 + B8(dlsb))
111 */
112 //
114 /*#define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) \
115 //+ ((unsigned long)B8(db2)<<16) \
116 //+ ((unsigned long)B8(db3)<<8) \
117 //+ B8(dlsb))
118 */
120 //B8(01010101) = 85
121 //B16(10101010,01010101) = 43605
122 //B32(10000000,11111111,10101010,01010101) = 2164238933
123 //*/
124 
125 #endif /* BITS_H_ */