当前位置: 代码迷 >> 综合 >> 位操作的几个函数
  详细解决方案

位操作的几个函数

热度:91   发布时间:2024-01-18 02:10:22.0

int Handfun(int reg,int state,int position) //将reg的第position位设置为state,
{
  return  state?(reg | (1<<position)):reg & (~(1 << position));
}

 

int judgement(int reg,int position) //返回reg的第position位是0还是1
{
    return (reg & (1 << position))>>position;
}

 

char* bitstream(void *s,int len) //打印s的二进制形式
{
    
char *t,*p;
    
int i,j,k=0; p=(char *)s;
     t
=(char*)calloc(len*8+1,sizeof(char));
    
for(j=0;j<len;j++)
    
for(i=8*sizeof(char);i>0;i--,t[k++]=((p[j]>>i)&1)+'0');
     puts(t);
//打印数组s的二进制形式
     return t;
}

  相关解决方案