从unsigned long long中提取前端位? [英] extracting front bits from an unsigned long long?

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

问题描述

假设我有一个无符号长的长。我想在这个值的前面'n'位提取

并将它们转换成整数。

例如,如果我提取前3位,我会在

0和7(= 2 ^ 3-1)之间得到一个int。有人可以帮忙吗?


我可以假设最大的返回值适合int。另外,

我正在使用大端PPC(AIX),以防万一。


理想情况下,我想实现一个原型如:

int extractFrontBits(unsigned long long value,int num_bits);

Hi, suppose I have an unsigned long long. I would like to extract
the front ''n'' bits of this value and convert them into an integer.
For example, if I extract the first 3 bits, I would get an int between
0 and 7 (=2^3-1). Could someone please help out?

I can assume the largest returned value fits in an int. Also,
I''m on a big-endian PPC (AIX), in case that matters.

Ideally, I''d like to implement a prototype like:
int extractFrontBits(unsigned long long value, int num_bits);

推荐答案

Digital Puer写道:
Digital Puer wrote:

假设我有一个unsigned long long。我想提取这个值的前面'n'位并将它们转换成一个整数。
例如,如果我提取前3位,我会得到一个int之间的
0和7(= 2 ^ 3-1)。有人可以帮忙吗?

Hi, suppose I have an unsigned long long. I would like to extract
the front ''n'' bits of this value and convert them into an integer.
For example, if I extract the first 3 bits, I would get an int between
0 and 7 (=2^3-1). Could someone please help out?




int three_bits = long_long_value& 7;


-

pete



int three_bits = long_long_value & 7;

--
pete


Digital Puer写道:
Digital Puer wrote:
例如,如果我提取前3位,我会得到一个int之间的
0和7(= 2 ^ 3-1)。有人可以帮帮忙吗?

我可以假设最大的返回值适合int。此外,
我正在使用大端PPC(AIX),以防万一。

理想情况下,我想实现一个原型,如:
int extractFrontBits(unsigned long long value,int num_bits);
Hi, suppose I have an unsigned long long. I would like to extract
the front ''n'' bits of this value and convert them into an integer.
For example, if I extract the first 3 bits, I would get an int between
0 and 7 (=2^3-1). Could someone please help out?

I can assume the largest returned value fits in an int. Also,
I''m on a big-endian PPC (AIX), in case that matters.

Ideally, I''d like to implement a prototype like:
int extractFrontBits(unsigned long long value, int num_bits);




我猜多长的是64位?无论如何,以下应该

工作:


frontbits =(original_value>>(sizeof(long long) - 3))& 0x03;


最后的掩码是将结果的msb归零,因为在某些系统上它没有被归零。


如果位数没有固定,那么它看起来像

如下:


frontbits = original_value >> (sizeof(long long) - num_bits);


但是掩盖会变得复杂。



I''m guessing long long is 64 bits? Regardless, the following should
work:

frontbits = (original_value >> (sizeof(long long) - 3)) & 0x03;

The masking at the end is to zero out the msb of the result since on
some systems it doesn''t get zeroed.

If number of bits is not fixed, then it would look something like the
following:

frontbits = original_value >> (sizeof(long long) - num_bits);

but the masking will get complicated.


sl ******* @ yahoo.com 写道:

数字Puer写道:

Digital Puer wrote:
假设我有一个unsigned long long。我想提取这个值的前面n位并将它们转换成整数。
例如,如果我提取前3位,
我会得到一个int在
0和7之间(= 2 ^ 3-1)。有人可以帮帮忙吗?
Hi, suppose I have an unsigned long long. I would like to extract
the front ''n'' bits of this value and convert them into an integer.
For example, if I extract the first 3 bits,
I would get an int between
0 and 7 (=2^3-1). Could someone please help out?


我猜长多是64位?无论如何,以下应该工作:
frontbits =(original_value>>(sizeof(long long) - 3))& 0x03;

I''m guessing long long is 64 bits? Regardless, the following should
work: frontbits = (original_value >> (sizeof(long long) - 3)) & 0x03;




你从价值中间得到2位。

Anything& 3,不会让你超过2位。

你将原始值移到右边

,低位比特消失。


-

pete



You''re getting 2 bits from the middle of the value.
Anything & 3, ain''t going to get you more than 2 bits.
You shift the original value to the right
and the low order bits disappear.

--
pete


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

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