如何从在C中提取一些特定位? [英] How to extract specific bits from a number in C?

查看:234
本文介绍了如何从在C中提取一些特定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取特定部分(无位)一个 C语言的数据类型。

I need to extract specific part (no of bits) of a short data type in C.

例如我有52504二进制为11001101000 11000,我想首先6(FROM LSB - > MSB即十进制011000 24)位和10位(11001101000十进制820)

For Example I have a binary of 52504 as 11001101000 11000 and I want First 6 ( FROM LSB --> MSB i.e 011000 decimal 24) bits and rest of 10 bits ( 11001101000 decimal 820).

同样我想这个功能太推广到提取特定没有给出开始和位结束(位即相当于大块一些十进制值)。

Similarly I want this function to be too generalized to extract specific no of bits given "start" and "end" (i.e chunks of bits equivalent with some decimal value).

我检查的其他职位,但这些都没有帮助,给出功能都没有太大一概而论。

I checked other posts, but those were not helpful, as given functions are not too much generalized.

我需要的东西,可以适用于C 数据类型。

I need something that can work for short data type of C.

我有大小2048字节的短阵。其中,每个像素是10比特。所以,我的16位由每个字节占用一些时间2个像素数据,有时3个像素的数据。

I am having the short array of size 2048 bytes. Where each Pixel is of 10 bits. So my 16 bit consisting each byte occupying some time 2 pixels data, sometimes 3 pixels data.

如同

(PIXEL:0,1)
 10位+ 6位

( PIXEL : 0,1 ) 10 BITS + 6 BITS

然后(PIXEL:1,2,3)
 4位(第1像素其余位)+ 10位+ 2位。

then ( PIXEL : 1,2,3 ) 4 BITS ( 1st pixels remaining bits ) + 10 BITS + 2 BITS.

等..this模式继续下去...
因此,所有我想提取每个像素,并让每个像素的整个阵列wholy在被占用对全字节(16位)喜欢..
1字节应含有1数据的像素,其他字节应包含在整个16位其他象素值等等等等。

and so on ..this pattern continues ... So, all I want to extract each pixel and make an entire array of having each pixels to be occupied wholy in on WHOLE BYTE ( of 16 bits ) like.. 1 byte should contain 1 DATA PIXEL, the other BYTE should contain other PIXEL value in whole 16 bits and so on so forth.

推荐答案

有你需要知道两积木搭建这个自己:

There are two building blocks that you need to know to build this yourself:


  • 获得 N 至少显著位需要构建的位掩码的有 N 那些在结束。你不喜欢这样:((1 <<;&LT; N)-1) 1 LT;&LT; ñ 2 ^ N :它有一个 1 N + 1 ST位置,之后全部为零。减人给你,你需要的掩码。

  • 丢弃 M 至少显著位是一个简单的右移: K&GT;&GT;中号

  • Getting N least significant bits requires constructing a bit mask with N ones at the end. You do it like this: ((1 << N)-1). 1 << N is 2 ^ N: it has a single 1 at the N+1st position, and all zeros after it. Subtracting one gives you the mask that you need.
  • Dropping M least significant bits is a simple shift to the right: k >> M

现在您从 M 切割出 N 算法变成了两个步骤:你转移原值 M 位到右侧,然后执行逐位与面具 NM 的。

Now your algorithm for cutting out from M to N becomes a two-step process: you shift the original value M bits to the right, and then perform a bit-wise AND with the mask of N-M ones.

#define LAST(k,n) ((k) & ((1<<(n))-1))
#define MID(k,m,n) LAST((k)>>(m),((n)-(m)))

int main() {
    int a = 0xdeadbeef;
    printf("%x\n",  MID(a,4,16));
    return 0;
}

该片段从4,包括削减了位,16个,独家,并打印当你运行它。位从零开始编号。

This fragment cuts out bits from 4, inclusive, to 16, exclusive, and prints bee when you run it. Bits are numbered from zero.

这篇关于如何从在C中提取一些特定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆