如何从32位数字中提取位 [英] How do I extract bits from 32 bit number

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

问题描述

由于我的一位同事正在休假,所以我对C的知识不多,并且陷入了一个问题.

I have do not have much knowledge of C and im stuck with a problem since one of my colleague is on leave.

我有一个32位数字,我必须从中提取位.我确实经历了几个线程,但是我仍然不清楚如何做到这一点.如果有人可以帮助我,我将非常有责任.

I have a 32 bit number and i have to extract bits from it. I did go through a few threads but im still not clear how to do so. I would be highly obliged if someone can help me.

这是我需要做的一个例子:

Here is an example of what i need to do:

假定十六进制数= 0xD7448EAB. 二进制= 1101 01 11 0100 0100 1000 11 10 1010 1011 我需要提取16位,然后输出该值.我想要10到25位.

Assume hex number= 0xD7448EAB. In binary= 1101 0111 0100 0100 1000 1110 1010 1011 I need to extract 16 bits, and output that value. I want bits 10 through 25.

低10位(十进制)将被忽略.即10 1010 1011被忽略.
高6位(溢出)将被忽略.即1101 01被忽略.

The lower 10 bits (Decimal) are ignored. i.e.,10 1010 1011 are ignored.
And the upper 6 bits (Overflow) are ignored. i.e., 1101 01 are ignored.

剩余的16位数据需要作为输出,即11 0100 0100 1000(需要使用斜体数字作为输出).

The remaining 16 bits of data needs to be the output which is 11 0100 0100 1000(numbers in italics are needed as the output).

这是一个示例,但是我将一直保持不同的十六进制数,并且我需要按照我的解释提取相同的位.

This was an example but i will keep getting different hex numbers all the time and i need to extract the same bits as i explained.

我该如何解决?

谢谢.

在此示例中,您将输出1101 0001 0010 0011,即0xD123或十进制53,539.

For this example you would output 1101 0001 0010 0011, which is 0xD123, or 53,539 decimal.

推荐答案

您需要 masks 来获取所需的位. 掩码是可用于以所需方式在位中进行筛选的数字(保留位,删除/清除位,修改数字等).您需要了解的是 AND,OR,XOR,NOT和shifting 操作.对于您所需要的,您只需要几个.

You need masks to get the bits you want. Masks are numbers that you can use to sift through bits in the manner you want (keep bits, delete/clear bits, modify numbers etc). What you need to know are the AND, OR, XOR, NOT, and shifting operations. For what you need, you'll only need a couple.

您知道移位: x << y 将位从 x * y位置移到左侧*.

You know shifting: x << y moves bits from x *y positions to the left*.

如何按顺序将x位设置为1: (1 << x) - 1

How to get x bits set to 1 in order: (1 << x) - 1

如何按顺序从y到y + x将x位设置为1, ((1 << x) -1) << y

How to get x bits set to 1, in order, starting from y to y + x: ((1 << x) -1) << y

上面是您需要的位的掩码.因此,例如,如果您想要16位0xD7448EAB,从10到25,则需要上面的x = 16和y = 10.

The above is your mask for the bits you need. So for example if you want 16 bits of 0xD7448EAB, from 10 to 25, you'll need the above, for x = 16 and y = 10.

现在要获取所需的位,只需 AND 上面带掩码的数字0xD7448EAB,您将得到仅包含所需位的 masked 0xD7448EAB.稍后,如果您要遍历每一个,则需要将结果向右移动10并一次处理每个位(在位置0).

And now to get the bits you want, just AND your number 0xD7448EAB with the mask above and you'll get the masked 0xD7448EAB with only the bits you want. Later, if you want to go through each one, you'll need to shift your result by 10 to the right and process each bit at a time (at position 0).

答案可能更长一些,但它比使用0xff或其他任何硬编码更好的设计.

The answer may be a bit longer, but it's better design than just hard coding with 0xff or whatever.

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

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