@@ -17,23 +17,66 @@ let binary = Binary(bytes: [0xDE, 0xAD]) // 1101 1110 1010 1101
17
17
```
18
18
19
19
``` swift
20
- // Bits
21
- binary.bit (0 ) as Int // 1
22
- binary.bit (1 ) as Int // 1
23
- binary.bit (2 ) as Bit // .Zero
24
- binary.bit (3 ) as Bit // .One
25
- binary.bits (0 , 16 ) // 57005
20
+ // Read first 4 bits, bit by bit
21
+ var binary = Binary (bytes : [0xDE , 0xAD ])
22
+ print (binary)
26
23
27
- // Bytes
28
- binary.byte (0 ) as Int // 222
29
- binary.bytes (0 , 2 ) as Int // 57005
24
+ let bit0 = binary.next (bits : 1 )
25
+ print (bit0) // 1
26
+
27
+ let bit1 = binary.next (bits : 1 )
28
+ print (bit1) // 1
29
+
30
+ let bit2 = binary.next (bits : 1 )
31
+ print (bit2) // 0
32
+
33
+ let bit3 = binary.next (bits : 1 )
34
+ print (bit3) // 1
35
+ ```
36
+
37
+ ``` swift
38
+ // Read next 4 bits, 2 x 2 bits
39
+ let bits4And5 = binary.next (bits : 2 )
40
+ print (bits4And5) // 3
41
+
42
+ let bits6And7 = binary.next (bits : 2 )
43
+ print (bits6And7) // 2
44
+ ```
45
+
46
+ ``` swift
47
+ // Set reading offset (cursor) back to starting position
48
+ binary.readingOffset = 0
30
49
```
31
50
32
- ## Todos
51
+ ``` swift
52
+ // Read first byte
53
+ let nextTwoBytes = binary.next (bytes : 2 )
54
+ print (nextTwoBytes) // [222, 173]
55
+ ```
33
56
34
- - [ ] Endianness flag
35
- - [ ] Tests
36
- - [ ] Documentation
57
+ ``` swift
58
+ // Read bit by position
59
+ let bit5 = binary.bit (5 )
60
+ print (bit5) // 1
61
+ ```
62
+
63
+ ``` swift
64
+ // Read byte by position
65
+ let byte1 = binary.byte (1 )
66
+ print (byte1) // 173
67
+ ```
68
+
69
+ ``` swift
70
+ // Read first 16 bits as Integer
71
+ let first16Bits = binary.bits (0 , 16 ) // 57005
72
+ print (first16Bits)
73
+ ```
74
+
75
+ ``` swift
76
+ // Read first two bytes as Integer
77
+ let firstTwoBytes = binary.bytes (0 , 2 ) as Int
78
+ print (firstTwoBytes) // 57005
79
+ ```
37
80
38
81
## License
39
82
0 commit comments