-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch16s01.c
More file actions
69 lines (60 loc) · 1.22 KB
/
ch16s01.c
File metadata and controls
69 lines (60 loc) · 1.22 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
#include <stdio.h>
int countbit(unsigned int x)
{
int result = 0;
while (x > 0) {
if (x & 1U) {
result++;
}
x >>= 1;
}
return result;
}
unsigned int multiply(unsigned int x, unsigned int y)
{
if (x == 0 || y == 0) {
return 0;
}
int result = 0;
int power = 0;
while (y > 0) {
if (y & 1U) {
result += x << power;
}
y >>= 1;
power++;
}
return result;
}
unsigned int rotate_right(unsigned int x, unsigned int num)
{
for (int i = 1; i <= num; i++) {
unsigned int temp = x & 1U;
x >>= 1;
x += temp << 31;
}
return x;
}
// void swap(unsigned int* a, unsigned int* b)
// {
// *a = ;
// }
int main()
{
// int i = 0xcffffff3;
// printf("%x\n", 0xcffffff3>>2); // 1100->0011
// printf("%x\n", i>>2); // 有符号数1100->1111
// printf("%d\n", countbit(0b111011101111));
// printf("%d\n", multiply(27, 50));
// printf("%x\n", rotate_right(0xdeadbeff, 16));
// swap
unsigned int a = 5, b = 10;
b &= 0x0000ffff;
a <<= 16;
b = a + b;
a = a + b;
b >>= 16;
a &= 0x0000ffff;
printf("%d, %d\n", a, b);
return 0;
}