-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordsize.c
More file actions
48 lines (37 loc) · 959 Bytes
/
Copy pathwordsize.c
File metadata and controls
48 lines (37 loc) · 959 Bytes
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
#include <stdio.h>
#include <stdlib.h>
/*
* NOTE: This function is supposed to get a compiler warning:
*
* wordsize.c: In function `int_size_is_32':
* wordsize.c:12: warning: left shift count >= width of type
*
*/
/* $begin wordsize */
/* The following code does not run properly on some machines */
int bad_int_size_is_32() {
/* Set most significant bit (msb) of 32-bit machine */
int set_msb = 1 << 31;
/* Shift past msb of 32-bit word */
int beyond_msb = 1 << 32;
/* set_msb is nonzero when word size >= 32
beyond_msb is zero when word size <= 32 */
return set_msb && !beyond_msb;
}
/* $end wordsize */
int find_word_size() {
int x = 1L;
int w = 0;
while (x) {
w++;
x <<= 1;
}
return w;
}
int main(int argc, char *argv[]) {
int w32 = bad_int_size_is_32();
int w = find_word_size();
printf("On this %d-bit machine, 32? = %s\n",
w, w32 ? "Yes" : "No");
return 0;
}