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

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

问题描述

我对 C 的了解不多,但我遇到了一个问题,因为我的一位同事正在休假.

I have do not have much knowledge of C and I'm 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 I'm 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 0111 0100 0100 1000 1110 1010 1011.
我需要提取 16 位,并输出该值.我想要位 10 到 25.

Assume hex number = 0xD7448EAB.
In binary = 1101 0111 0100 0100 1000 1110 1010 1011.
I need to extract the 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 11(需要斜体数字作为输出).

The remaining 16 bits of data needs to be the output which is 11 0100 0100 1000 11 (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.

我该如何解决这个问题?
谢谢.

How do I solve this?
Thank you.

对于本例,您将输出 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 和 shift 操作.对于你需要的,你只需要一对.

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

如何将x位设置为1,从y到y + x的顺序: ((1 <

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天全站免登陆