在C中如何将数组中的元素移动到变量中
我有一个数组as(用0和1填充)->ArrayWithContent[5]={1,0,1,1}; 现在我想把它转换成一个变量,这样我就可以读出它的总值 0001 0011=19在C中如何将数组中的元素移动到变量中,c,arrays,for-loop,bit-manipulation,C,Arrays,For Loop,Bit Manipulation,我有一个数组as(用0和1填充)->ArrayWithContent[5]={1,0,1,1}; 现在我想把它转换成一个变量,这样我就可以读出它的总值 0001 0011=19 for(i=0; i<5; i++) { OneValue = ArrayWithContent[i]; Variable = OneValue; Variable >>= 1; // Send the zero or one to right....
for(i=0; i<5; i++)
{
OneValue = ArrayWithContent[i];
Variable = OneValue;
Variable >>= 1; // Send the zero or one to right.... continue to fill it up
}
for(i=0;i>=1;//将0或1发送到右侧…继续填充
}
显示变量的内容我现在希望它显示值19
我知道我做错了,正确的方法是什么?指针和地址?Variable=0;
Variable = 0;
for (i = 0; i < 5; i++)
{
Variable = (Variable << 1) | ArrayWithContent[i];
}
对于(i=0;i<5;i++)
{
变量=(变量<代码>变量=0;
对于(i=0;i<5;i++)
{
变量=(变量这是您的循环,已修复:
for(i=0; i<5; i++)
{
OneValue = ArrayWithContent[i];
Variable <<= 1; // You want to shift to the left to keep the previous value.
Variable |= OneValue; // You need to OR the value, else you'd erase the previous value.
}
for(i=0;i这是您的循环,已修复:
for(i=0; i<5; i++)
{
OneValue = ArrayWithContent[i];
Variable <<= 1; // You want to shift to the left to keep the previous value.
Variable |= OneValue; // You need to OR the value, else you'd erase the previous value.
}
for(i=0;i如果您的数据采用big-endian格式,
…将每个值移动正确的量,或将所有值一起移动
value = 0;
for (i = 0; i < nelems; i++) {
value |= (ArrayWithContent[i] << (nelems - i - 1));
}
value=0;
对于(i=0;i 值|=(带内容的数组[i]如果您的数据为big-endian格式,
…将每个值移动正确的量,或将所有值一起移动
value = 0;
for (i = 0; i < nelems; i++) {
value |= (ArrayWithContent[i] << (nelems - i - 1));
}
value=0;
对于(i=0;i