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
最新推荐文章于 2024-09-04 00:38:24 发布
本文介绍了如何实现8bit数据的指定位置0/1操作,以及两种字符串右循环位移的方法:逐步移位法和三步翻转法。此外,还提供了一个模拟实现atoi()的函数,能够将字符串转换为整数。
摘要由CSDN通过智能技术生成