一个简单的位操作查询 [英] A simple bit operation query

查看:64
本文介绍了一个简单的位操作查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个号码,我想找到等于或大于n的8

的最接近的数字倍数。即。如果n = 25,它应该是32.如果n是42

它应该给我42.(基本上是8 *(n / 8))。但是,我必须使用按位

运算符。


有谁可以建议可以做什么?


Regads ,

Subhransu

解决方案

sfs说:


大家好,


我有一个号码,我想找到8

等于或大于n的最接近的数字倍数。即。如果n = 25,它应该是32.如果n是42

它应该给我42.



42不是多个8.


(基本上是8 *(n / 8))。但是,我必须使用按位

运算符。


任何人都可以建议可以做什么?



如果你想要8 *(n / 8),这很容易:n& = ~7;

但我觉得你其实想要((n + 7)/ 8)* 8,这有点棘手。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。


sfs写道:


大家好,


我有一个号码,我想找到8

等于或大于的最接近的数字倍数ñ。即。如果n = 25,它应该是32.如果n是42

它应该给我42.(基本上是8 *(n / 8))。但是,我必须使用按位

运算符。


任何人都可以建议可以做什么?



根据你的例子和描述,你的意思是,8 *(n / 8)+ 8


最后三位作为零。所以,


val = n& ~7;

if(val< n)

n + = 8;


应该可以工作。
< blockquote class =post_quotes>
>

Regads,

Subhransu


p_cricket _... @ yahoo.co.in写道:


sfs写道:


大家好,


我有一个号码,我想找到8

等于或大于n的最接近的数字倍数。即。如果n = 25,它应该是32.如果n是42

它应该给我42.(基本上是8 *(n / 8))。但是,我必须使用按位

运算符。


任何人都可以建议可以做什么?




根据你的例子和描述,你的意思是,8 *(n / 8)+ 8



糟糕.. + 8部分仅在n不能被8整除的情况下。

下面的代码有效。我希望你没有

限制仅限必须使用按位运算符。


>

8 =" 1000"用二进制表示时。任何8的倍数都将

最后三位作为零。所以,


val = n& ~7;

if(val< n)

n + = 8;


应该可以工作。
< blockquote class =post_quotes>

Regads,

Subhransu


Hi All,

I have a number and I want to find the closest number multiple of 8
equal to or greater than n. ie. if n = 25 it should be 32. If n is 42
it should give me 42. (Basically 8 * (n/8)). But, I have to use bitwise
operators.

Can anyone suggest what can be done?

Regads,
Subhransu

解决方案

sfs said:

Hi All,

I have a number and I want to find the closest number multiple of 8
equal to or greater than n. ie. if n = 25 it should be 32. If n is 42
it should give me 42.

42 isn''t a multiple of 8.

(Basically 8 * (n/8)). But, I have to use bitwise
operators.

Can anyone suggest what can be done?

If 8 * (n / 8) is all you want, it''s easy enough: n &= ~7;

But I think you actually want ((n + 7) / 8) * 8, which is a bit trickier.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


sfs wrote:

Hi All,

I have a number and I want to find the closest number multiple of 8
equal to or greater than n. ie. if n = 25 it should be 32. If n is 42
it should give me 42. (Basically 8 * (n/8)). But, I have to use bitwise
operators.

Can anyone suggest what can be done?


Going by your example and description, you mean, 8 * (n/8) + 8

8 = "1000" when represented in binary. Any multiple of 8 will have
last three bits as zeros. So,

val = n & ~7;
if (val < n)
n += 8;

Should work.

>
Regads,
Subhransu


p_cricket_...@yahoo.co.in wrote:

sfs wrote:

Hi All,

I have a number and I want to find the closest number multiple of 8
equal to or greater than n. ie. if n = 25 it should be 32. If n is 42
it should give me 42. (Basically 8 * (n/8)). But, I have to use bitwise
operators.

Can anyone suggest what can be done?



Going by your example and description, you mean, 8 * (n/8) + 8

Oops .. the + 8 part is only if n is not already divisible by 8.
The code below works though. I hope you do not have
restrictions that "only" bitwise operators must be used.

>
8 = "1000" when represented in binary. Any multiple of 8 will have
last three bits as zeros. So,

val = n & ~7;
if (val < n)
n += 8;

Should work.


Regads,
Subhransu


这篇关于一个简单的位操作查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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