-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbwtest.c
More file actions
135 lines (119 loc) · 3.16 KB
/
bwtest.c
File metadata and controls
135 lines (119 loc) · 3.16 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* File: bwtest.c
* Purpose: Test bitwriter.c
* Author: Kerry Veenstra
*/
#include "bitwriter.h"
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
/*
* Create a file.
*/
BitWriter *buf = bit_write_open("bwtest.out");
/*
* Write the bits to a file using all of the bitwriter.c functions.
* We deliberately choose bits that will create a text file.
*/
/*
* 0x41 'A' 01000001
* 0x42 'B' 01000010
*
* 'A' followed by 'B' is 0x41 followed by 0x42.
* In binary that's 01000010 01000001, which can be written as bits/byte.
* ^^^^^^^\_______/^
* | | ^ |___ start with this bit 1
* | ... | |_______ then the byte 00100000 = 0x20
* | |_____________ etc.
* |___________________
*/
bit_write_bit(buf, 1);
bit_write_uint8(buf, 0x20);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
/*
* 0x43 'C' 01000011
* 0x44 'D' 01000100
* 0x45 'E' 01000101
* 0x46 'F' 01000110
* 0x47 'G' 01000111
*
* 01000111 01000110 01000101 01000100 01000011
* ^^^^^\__________________________________/^^^
* 11101000 11001000 10101000 10001000
*/
bit_write_bit(buf, 1);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_uint32(buf, 0xe8c8a888);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
/*
* 0x48 'H' 01001000
* 0x49 'I' 01001001
* 0x4a 'J' 01001010
*
* 01001010 01001001 01001000
* ^^^^^^\_______/^^ ^^^^^^^^
* 10010010 = 0x92
*/
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_uint8(buf, 0x92);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
/*
* 0x4b 'K' 01001011
* 0x4c 'L' 01001100
* 0x4d 'M' 01001101
* 0x4e 'N' 01001110
*
* 01001110 01001101 01001100 01001011
* ^\_______/\_______/\_______/^^^^^^^
* 10011100 10011010 10011000
* 0x9c 0x9a 0x98
*/
bit_write_bit(buf, 1);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_bit(buf, 0);
bit_write_bit(buf, 0);
bit_write_bit(buf, 1);
bit_write_uint8(buf, 0x98);
bit_write_uint8(buf, 0x9a);
bit_write_uint8(buf, 0x9c);
bit_write_bit(buf, 0);
/*
* '\n' = 10
*/
bit_write_uint8(buf, 10);
bit_write_close(&buf);
printf("File bwtest.out should contain \"ABCDEFGHIJKLMN\".\n");
printf("(It would be good to delete bwtest.out before running bwtest.\n");
return 0;
}