如何忽略变量中的位
How to ignore bits in a variable
我想知道如何删除位值中的位。
我收到一个 10 位值(第 0 位到第 9 位),我必须发送一个忽略接收值的第 0 位、第 2 位、第 4 位和第 6 位的变量,然后我的变量将是:位 987531 。 我能怎么做 ?我听说过掩码位我真的不知道如何使用它,即使我知道掩码是 0x55
谢谢你帮助我
像下面这样手动创建位:
//Option 1
uint8_t result = 0;
result |= (inputValue >> 1) & 1;
result |= ((inputValue >> 3) & 1) << 1;
result |= ((inputValue >> 5) & 1) << 2;
result |= ((inputValue >> 7) & 1) << 3;
result |= ((inputValue >> 8) & 1) << 4;
result |= ((inputValue >> 9) & 1) << 5;
我闻到嵌入式的味道 :) 我不保证可移植性(但对于我知道的大多数编译器,它会做到)
union
{
struct
{
unsigned b0 : 1;
unsigned b1 : 1;
unsigned b2 : 1;
unsigned b3 : 1;
unsigned b4 : 1;
unsigned b5 : 1;
unsigned b6 : 1;
unsigned bx : 3;
}
uint16_t raw;
}raw;
raw.raw = value;
uint8_t without_skipped = raw.b1 | (raw.b3 << 1) | (raw.b5 << 2) | (raw.bx << 3) ;
这是小端
对于大端,将结构和未命名的位域反转为填充
始终使用 5 位的解决方案可能是
new_value = ((data & 0x002) >> 1) |
((data & 0x008) >> 2) |
((data & 0x020) >> 3) |
((data & 0x080) >> 4) |
((data & 0x200) >> 5);
但这里有另一种解决方案,它不是使用固定位数(即在您的情况下为 5),而是使用允许您指定要保留的位数的函数。
可能是这样的:
#include <stdio.h>
#include <stdlib.h>
unsigned keepOddBits(const unsigned data, const unsigned number_of_bits_to_keep)
{
unsigned new_value = 0;
unsigned mask = 0x2;
int i;
for (i=0; i < number_of_bits_to_keep; ++i)
{
if (mask & data)
{
new_value = new_value | ((mask & data) >> (i + 1));
}
mask = mask << 2;
}
return new_value;
}
int main()
{
printf("data 0x%x becomes 0x%x\n", 0x3ff, keepOddBits(0x3ff, 5));
printf("data 0x%x becomes 0x%x\n", 0x2aa, keepOddBits(0x2aa, 5));
printf("data 0x%x becomes 0x%x\n", 0x155, keepOddBits(0x155, 5));
return 0;
}
将输出:
data 0x3ff becomes 0x1f
data 0x2aa becomes 0x1f
data 0x155 becomes 0x0
将 main
更改为请求 3 位而不是 5 位,例如:
int main()
{
printf("data 0x%x becomes 0x%x\n", 0x3ff, keepOddBits(0x3ff, 3));
printf("data 0x%x becomes 0x%x\n", 0x2aa, keepOddBits(0x2aa, 3));
printf("data 0x%x becomes 0x%x\n", 0x155, keepOddBits(0x155, 3));
return 0;
}
将输出:
data 0x3ff becomes 0x7
data 0x2aa becomes 0x7
data 0x155 becomes 0x0
我想知道如何删除位值中的位。
我收到一个 10 位值(第 0 位到第 9 位),我必须发送一个忽略接收值的第 0 位、第 2 位、第 4 位和第 6 位的变量,然后我的变量将是:位 987531 。 我能怎么做 ?我听说过掩码位我真的不知道如何使用它,即使我知道掩码是 0x55
谢谢你帮助我
像下面这样手动创建位:
//Option 1
uint8_t result = 0;
result |= (inputValue >> 1) & 1;
result |= ((inputValue >> 3) & 1) << 1;
result |= ((inputValue >> 5) & 1) << 2;
result |= ((inputValue >> 7) & 1) << 3;
result |= ((inputValue >> 8) & 1) << 4;
result |= ((inputValue >> 9) & 1) << 5;
我闻到嵌入式的味道 :) 我不保证可移植性(但对于我知道的大多数编译器,它会做到)
union
{
struct
{
unsigned b0 : 1;
unsigned b1 : 1;
unsigned b2 : 1;
unsigned b3 : 1;
unsigned b4 : 1;
unsigned b5 : 1;
unsigned b6 : 1;
unsigned bx : 3;
}
uint16_t raw;
}raw;
raw.raw = value;
uint8_t without_skipped = raw.b1 | (raw.b3 << 1) | (raw.b5 << 2) | (raw.bx << 3) ;
这是小端
对于大端,将结构和未命名的位域反转为填充
始终使用 5 位的解决方案可能是
new_value = ((data & 0x002) >> 1) |
((data & 0x008) >> 2) |
((data & 0x020) >> 3) |
((data & 0x080) >> 4) |
((data & 0x200) >> 5);
但这里有另一种解决方案,它不是使用固定位数(即在您的情况下为 5),而是使用允许您指定要保留的位数的函数。
可能是这样的:
#include <stdio.h>
#include <stdlib.h>
unsigned keepOddBits(const unsigned data, const unsigned number_of_bits_to_keep)
{
unsigned new_value = 0;
unsigned mask = 0x2;
int i;
for (i=0; i < number_of_bits_to_keep; ++i)
{
if (mask & data)
{
new_value = new_value | ((mask & data) >> (i + 1));
}
mask = mask << 2;
}
return new_value;
}
int main()
{
printf("data 0x%x becomes 0x%x\n", 0x3ff, keepOddBits(0x3ff, 5));
printf("data 0x%x becomes 0x%x\n", 0x2aa, keepOddBits(0x2aa, 5));
printf("data 0x%x becomes 0x%x\n", 0x155, keepOddBits(0x155, 5));
return 0;
}
将输出:
data 0x3ff becomes 0x1f
data 0x2aa becomes 0x1f
data 0x155 becomes 0x0
将 main
更改为请求 3 位而不是 5 位,例如:
int main()
{
printf("data 0x%x becomes 0x%x\n", 0x3ff, keepOddBits(0x3ff, 3));
printf("data 0x%x becomes 0x%x\n", 0x2aa, keepOddBits(0x2aa, 3));
printf("data 0x%x becomes 0x%x\n", 0x155, keepOddBits(0x155, 3));
return 0;
}
将输出:
data 0x3ff becomes 0x7
data 0x2aa becomes 0x7
data 0x155 becomes 0x0