Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌入式c语言中的位提取_C - Fatal编程技术网

嵌入式c语言中的位提取

嵌入式c语言中的位提取,c,C,我正在从给定字节中提取位。我有32位的二进制文件0XFA73DECB 11111 1010 0111 0011 1101 1110 1100 1011。现在如何提取第7到22位?有人帮我吗?下面的代码首先将位提取为另一个int,然后以二进制输出 #include <stdio.h> #include <stdlib.h> unsigned int a = 0xfa73decb; int main() { unsigned int Bits = 0b00000011

我正在从给定字节中提取位。我有32位的二进制文件0XFA73DECB 11111 1010 0111 0011 1101 1110 1100 1011。现在如何提取第7到22位?有人帮我吗?

下面的代码首先将位提取为另一个int,然后以二进制输出

#include <stdio.h>
#include <stdlib.h>
unsigned int a = 0xfa73decb;
int main()
{
    unsigned int Bits = 0b00000011111111111111110000000000;
    unsigned int Extract = (a & Bits) >> 10;
    char Bin[20];
    _itoa(Extract, Bin, 2);
    printf("%s\n", Bin);
    while (1);
}
#包括
#包括
无符号整数a=0xfa73decb;
int main()
{
无符号整数位=0B0000001111110000000000;
无符号整数提取=(a和位)>>10;
char-Bin[20];
_itoa(提取物,Bin,2);
printf(“%s\n”,Bin);
而(1),;
}

_itoa(下划线是因为我使用的是Visual Studio,典型的itoa不推荐使用)意味着将整数提取转换为基数为2的字符串,并将其存储到字符串库中,以便动态读取输入以提取位

#include <stdio.h>
#include <stdlib.h>
unsigned int a = 0xfa73decb;

int main()
{
   int msb;
   int lsb;
   unsigned int Extract;

   printf("size of unsigned int = %d\n", sizeof(unsigned int));     // sizeof int : 4 bytes (32 bits)

   scanf("%d", &lsb);              // as per your requirement: 10
   scanf("%d", &msb);              // as per your requirement: 26

   msb = (8*sizeof(unsigned int)) - msb;         // msb = 32 - 26 ; // 6 
   lsb = lsb + msb ;                             // lsb = 10 + 6 ;  // 16

   Extract = (a << msb);                        // shifting 6 times left
   printf("After Left shift = 0x%0x\n", Extract);
   Extract = Extract >> lsb;                   // shifting 16 times right
   printf("After Right shift = 0x%0x\n", Extract);

}
输出:

size of unsigned int = 4
After Left shift = 0x9cf7b2c0
After Right shift = 0x9cf7

我想你突出显示的是7到22位,而不是10到25位哦!是的。现在我变了。你说的拔牙是什么意思?您可以按7移位并屏蔽高位,但我不知道您想做什么?我想让这些位保持原样,保持关闭状态。谢谢您,Eswaran
size of unsigned int = 4
After Left shift = 0x9cf7b2c0
After Right shift = 0x9cf7