如何解决如何从c中的位读取char?
我想通过移动字符(从而更改其值)来修改字符并返回结果,而不是先前的字符:
#include <stdio.h>
#include <stdlib.h>
int main(){
char * str = calloc(4,sizeof(char));
printf("what bit to set [0-31]\n");
int a;
scanf(" %i",&a);
int byte = a/8;
int offset = a%8;
str[byte] = (str[byte] >> offset) & (char)255; //how to return the modified char?
for(int i =0; i<4 ; i++)
{
for (int j =0; j<8 ; j++)
printf("%i",(str[i] >> (7-j)) & 1); //print the results
printf("\n");
}
}
输出:
what bit to set [0-31]
20
00000000
00000000
00000000 // I want 00001000
00000000
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。