1.实现一个8bit数据的指定位的置0或置1操作,其他位不变;
#include<stdio.h>
#include<assert.h>
void bit_set(unsigned char *p_data,unsigned char pos,int flag)
{
assert(p_data);
assert(pos>=1&&pos<=8);//置位操作只能在1-8之间
if(flag==1)//置1
{
*p_data|=(1<<(pos-1));
}
else if(flag==0)//置0
{
*p_data&=~(1<<(pos-1));
}
}
int main()
{
unsigned char c=0;
bit_set(&c,3,0);
printf("%d\n",c);
return 0;
}
15:00001111
00001011
2.实现字符串右循环位移函数,比如:
"abcdefghi"循环右移两位就是"hiabcdefg".
方法一:逐步移位法
#include<stdio.h>
#include<assert.h>
#include<string.h>
void RightLoopMove(char *pStr,unsigned short steps)
{
int len = strlen(pStr);
assert(pStr);
while(steps--)
{
char tmp=pStr[len-1];//保存最后一个变量
int i=0;
for(i=len-1;i>0;i--)//将前面的变量依次向后挪一位
{
pStr[i]=pStr[i-1];
}
pSt
模拟实现atoi(),字符串循环位移,8bit位指定位的置0或置1
最新推荐文章于 2020-09-28 00:43:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)