-
Notifications
You must be signed in to change notification settings - Fork 4
/
Bits.bt
64 lines (55 loc) · 2.06 KB
/
Bits.bt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// -*- mode: c;-*-
/*
We don't actually know what the main bank does / us.
Here are some types relating to it, though.
TODO: Find the main bank definition(s).
*/
/*
list of would-be-nice-to-have-functions
* FindFirstBitO[n|ff](From[Lef|Righ]t)
* BiggestNumberForType
* GetMaskForBits(ubyte d[]{0,1,1,0})
* FindLowestCommonMask(ubyte d[])
* BitWeld(uint64 a, uint64 b, int start=-1) //append bits from a in front of b (at first available bit)
*/
// from stackexchange or some crap
int FlipIntBits(int theNumber, int numBits) {
local int mask = 1;
local int i;
for(i = 1; i < numBits; ++i)
mask |= mask << 1;
return ~theNumber & mask;
}
// want your own byte array? check it out:
// http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html
typedef struct {
byte bit1 : 1;
byte bit2 : 1;
byte bit3 : 1;
byte bit4 : 1;
byte bit5 : 1;
byte bit6 : 1;
byte bit7 : 1;
byte bit8 : 1;
} BitEight <read=BitEightRead, size=1>;
string BitEightRead(BitEight &be){
local string s;
SPrintf(s, "%d %d %d %d, %d %d %d %d", be.bit1, be.bit2, be.bit3, be.bit4, be.bit5, be.bit6, be.bit7, be.bit8);
return s;
};
uint64 ReadUShortBits(uint64 pos, uint64 bitStart, uint64 readSize){
local ushort data = ReadUShort(pos);
// we can't adjust the size or type of what we read, so we can hardcode the mask base
// this sure beats writing a geometric function to read bits
local ushort mask = 0xFFFF >> (sizeof(data)*8 - readSize);
local ushort outvalue = (( data >> (sizeof(data)*8 - (readSize + bitStart)) ) & mask);
return outvalue;
}
uint64 ReadUByteBits(uint64 pos, uint64 bitStart, uint64 readSize){
local ubyte data = ReadUByte(pos);
// we can't adjust the size or type of what we read, so we can hardcode the mask base
// this sure beats writing a geometric function to read bits
local ubyte mask = 0xFF >> (sizeof(data)*8 - readSize);
local ubyte outvalue = (( data >> (sizeof(data)*8 - (readSize + bitStart)) ) & mask);
return outvalue;
}