Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96112
License: OTHER
1
/* Example code for Think OS.
2
3
Copyright 2014 Allen Downey
4
License: GNU GPLv3
5
6
*/
7
8
#include <stdio.h>
9
10
11
void print_float() {
12
union {
13
float f;
14
unsigned int u;
15
} p;
16
17
p.f = -13.0;
18
unsigned int sign = (p.u >> 31) & 1;
19
unsigned int exp = (p.u >> 23) & 0xff;
20
21
unsigned int coef_mask = (1 << 23) - 1;
22
unsigned int coef = p.u & coef_mask;
23
24
printf("%d\n", sign);
25
printf("%d\n", exp);
26
printf("%x\n", coef);
27
}
28
29
30
void print_float2() {
31
union {
32
float f;
33
struct {
34
unsigned int sign:1;
35
unsigned int exp:8;
36
unsigned int coef:23;
37
} u;
38
} p;
39
40
p.f = -13.0;
41
42
printf("%d\n", p.u.sign);
43
printf("%d\n", p.u.exp);
44
printf("%x\n", p.u.coef);
45
}
46
47
48
int main()
49
{
50
int x;
51
52
x = 12 & 10;
53
printf("%d\n", x);
54
55
x = 12 | 10;
56
printf("%d\n", x);
57
58
x = 12 ^ 10;
59
printf("%d\n", x);
60
61
x = (12 ^ -1) + 1;
62
printf("%d\n", x);
63
64
print_float();
65
print_float2();
66
}
67
68
69