-
Notifications
You must be signed in to change notification settings - Fork 274
/
bit.c
114 lines (77 loc) · 1.92 KB
/
bit.c
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
//
// bit.c
// Algorithms - Bit operations
//
// Created by YourtionGuo on 17/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <string.h>
#include "bit.h"
#pragma mark - Public
int bit_get(const unsigned char *bits, int pos)
{
unsigned char mask;
int i;
/// 设置位获取掩码
mask = 0x80;
for (i = 0; i < (pos % 8); i++) {
mask = mask >> 1;
}
/// 获取位
return (((mask & bits[(int)(pos / 8)]) == mask) ? 1 : 0);
}
void bit_set(unsigned char *bits, int pos, int state)
{
unsigned char mask;
int i;
/// 设置位获取掩码
mask = 0x80;
for (i = 0; i < (pos % 8); i++) {
mask = mask >> 1;
}
/// 设置位
if (state) {
bits[pos / 8] = bits[pos / 8] | mask;
} else {
bits[pos / 8] = bits[pos / 8] & (~mask);
}
return;
}
void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned char *bitsx, int size)
{
int i;
/// 按位计算两个缓存区的异或值
for (i = 0; i < size; i++) {
if (bit_get(bits1, i) != bit_get(bits2, i)) {
bit_set(bitsx, i, 1);
} else {
bit_set(bitsx, i, 0);
}
}
return;
}
void bit_rot_left(unsigned char *bits, int size, int count)
{
int fbit = 0, lbit, i, j;
/// 向左轮转缓冲区特定位数
if (size > 0) {
for (j = 0; j < count; j++) {
for (i = 0; i <= ((size - 1) / 8); i++) {
/// 获取即将被替换的位的值
lbit = bit_get(&bits[i], 0);
if (i == 0) {
/// 将第一个位保存起来
fbit = lbit;
} else {
/// 将前一个字节最右边的位移到当前字节的最左边
bit_set(&bits[i - 1], 7, lbit);
}
/// 将特定位向左移动
bits[i] = bits[i] << 1;
}
/// 将其最右边的位移动到首字节的最高位上
bit_set(bits, size - 1, fbit);
}
}
return;
}