UTF-8, untabify, whitespaces.
[binary-types.git] / README-bitfield
1
2 > My only problem is with DEF-BITFIELD. All other BINARY-TYPES
3 > features are intuitive and easy to use.
4
5 Hi, you are right that DEF-BITFIELD is poorly documented. I think
6 that's because it's a bit complex and I'm not quite confident it is
7 the way it should be. Anyways, here are a couple of examples:
8
9 (define-bitfield r-info (u32)
10               (((:enum :byte (8 0))
11                  r-386-none     0
12                  r-386-32       1
13                  r-386-pc32     2
14                  r-386-got32    3
15                  r-386-plt32    4
16                  r-386-copy     5
17                  r-386-glob-dat 6
18                  r-386-jmp-slot 7
19                  r-386-relative 8
20                  r-386-gotoff   9
21                  r-386-gotpc    10)
22                 ((:numeric r-sym 24 8))))
23
24 This declares R-INFO to be an unsigned 32-bit number, divided into two
25 fields. The first field resides in bits 0-7, and is one of the values
26 r-386-xx. The second field is a numeric value that resides in bits
27 8-23. So this type R-INFO may for example have symbolic value
28 (r-386-pc32 (r-sym . 1)), which translates to a numeric value of
29  (logior 2 1<<8)) = 258.
30
31 Another example:
32
33 (define-bitfield p-flags (u8)
34                 (((:bits)
35                   pf-x 0
36                   pf-w 1
37                   pf-r 2)))
38
39 Here P-FLAGS has just one bit-field, where bit 0 is named PF-X, bit 1
40 is named PF-W etc. So the value (PF-X PF-R) maps to 5.
41
42 As you may see by now, DEF-BITFIELD divides a numeric base-type
43 (typically an unsigned integer) into a number of fields, where each
44 field is one of :BITS for bitmaps, :ENUM for an enumerated field
45 (takes an optional :BYTE <bytespec>), and finally :NUMERIC <byte-size>
46 <byte-pos> for a subfield that is a number.
47
48 Hope this helps.
49
50 -- 
51 Frode Vatvedt Fjeld
52