c - Bitshift 字符数组

标签 c arrays char bit-manipulation bit-shift

如果我想对整数 5 进行位移乘以 3,所以 int a = 5; int b = a << 3; ,结果将是 40十进制为 510140101000 .

但是,如果我有以下 char 怎么办?大批: 00000 00101通过向左移动三位,我希望结果为 00001 01000 .所以我想适应 0的填充。你有什么建议?

最佳答案

如果你指的是一个实际的 char 数组,你可以使用 memmove()memset():

char str[] = "0000000101";

int shift = 3;
int length = strlen(str);

memmove(str, str + shift,length - shift);
memset(str + length - shift,'0',shift);

//  Result:
//  "0000101000"

关于c - Bitshift 字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8085521/

相关文章:

c - Makefile 编译对象和移动它们时出现问题

c - Linux 中 WaitForSingleObject() 和 ResetEvent() 的等效性

c - 使用 C 从文件中读取数据以创建数组

c# - 确定大型整数列表中常见元素计数的最有效方法

javascript - 按预定义的顺序对 javascript 数组进行排序

Javascript表多维

C# 错误 : operator '!=' cannot be applied to operands of type 'char' and 'string'

c - 打印数组元素的代码如何工作?

c++ - 比较字符会导致总线错误

c - 在函数内部使用指针时出错