开始写信安大作业,考虑了下分段位的实现,想到C实现位操作,不过有点慢就是,初步测试通过,估计这是最基础的功能了吧。 🙂
int dec_bit_set( unsigned char * s, int x, int mode)
{
/*
* set 1 "|"
* x 0 1 2 3 4 5 6 7
* code 80H 40H 20H 10H 08H 04H 02H 01H
* num 128 64 32 16 8 4 2 1
*
* set 0 "&"
* x 0 1 2 3 4 5 6 7
* code 7FH BFH DFH EFH F7H FBH FDH FEH
* num 127 191 223 239 247 251 253 254
* */
unsigned char A[8] = {128, 64, 32, 16, 8, 4, 2, 1};
unsigned char B[8] = {127, 191, 223, 239, 247, 251, 253, 254};
if ((mode != 0)&&(mode != 1)) return 0;
#ifdef DEBUG
printf(" bit_set: %p x = %d mode = %d\n", s, x, mode);
#endif
while (x>=8)
{
s++;
x -= 8;
}
unsigned char i = *s;
if (mode == 1) i = i | A[x];
if (mode == 0) i = i & B[x];
*s = i;
return 1;
}