位操作 [英] Bit manipulation

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

问题描述



我有一个问题,我必须写一个函数,它需要

的位置和要修改的位数。

所以myfunc(val,pos,size)其中val = 11000001,pos = 3且size = 2

并且赞美这些位。

结果就是这样。 11000111


我无法解决这个问题。

你能帮忙吗?

谢谢,

kapil

Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

I am not able to figure this one out.
Can you help ?
Thanks,
kapil

推荐答案

2003年9月1日20:07:24 -0700, kh ********* @ yahoo.com (Kapil Khosla)

在comp.lang.c中写道:
On 1 Sep 2003 20:07:24 -0700, kh*********@yahoo.com (Kapil Khosla)
wrote in comp.lang.c:

我有一个问题,我必须编写一个函数,它需要修改位置和位数。
所以myfunc( val,pos,size)其中val = 11000001,pos = 3,size = 2
并赞美这些位。
结果就是这样。 11000111

我无法想出这个。
你能帮忙吗?
谢谢,
kapil
Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

I am not able to figure this one out.
Can you help ?
Thanks,
kapil




这里的一般规则是,除非他们告诉我们他们已经做了一些努力,否则我们不会为他们编写人们的代码。

首先是自己。


以完全标准的,便宜的方式进行实际操作非常棘手,但如果你将便携性限制在

在无符号整数中没有填充位的实现

类型。


在任何情况下,我都有一个短函数将在任何平台上工作

满足上述限制(并非多少限制)。


所以你告诉我们你的,我会告诉你我的。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C- faq / top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq



The general rule around here is that we don''t write people''s code for
them, unless they show us that they have made some effort at doing it
themselves first.

This is actually rather tricky to do in a completely standard,
portable way, but quite simple if you limit portability to
implementations that have no padding bits in their unsigned integer
types.

In any case, I have a short function that will work on any platform
that meets the limitation (not much of a limitation) above.

So you show us yours and I''ll show you mine.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq




" Kapil Khosla" < KH ********* @ yahoo.com>在留言中写道

news:91 ************************** @ posting.google.c om ...

"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...

我有一个问题,我必须写一个函数,它需要修改位置和位数。
所以myfunc(val, pos,size)其中val = 11000001,pos = 3,size = 2
并补充这些位。
结果就是这样。 11000111


类似的东西。


long myfunc(long var,int pos,int size)

{

长面具= 0;

长tmp;

int i;


for(i = pos; i> pos-size; i--)

{

tmp = 1<< (i - 1);

mask = mask | tmp;


}


返回var ^ mask;


}


我无法解决这个问题。
你能帮忙吗?
谢谢,
kapil
Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

Something like that.

long myfunc(long var, int pos, int size)
{
long mask = 0;
long tmp;
int i;

for(i=pos; i>pos-size; i--)
{
tmp = 1 << (i - 1);
mask = mask | tmp;

}

return var ^ mask;

}

I am not able to figure this one out.
Can you help ?
Thanks,
kapil




-

Jeff



--
Jeff


2003年9月3日星期三09:34:49 + 0800,杰夫 < no ***** @ notexist.com>

写道:
On Wed, 3 Sep 2003 09:34:49 +0800, "Jeff" <no*****@notexist.com>
wrote:

" Kapil Khosla" < KH ********* @ yahoo.com>在消息中写道
新闻:91 ************************** @ posting.google.c om ...

"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...

我有一个问题,我必须写一个函数,它需要修改位置和位数。
所以myfunc(val,pos,size)其中val = 11000001,pos = 3且size = 2
并补充这些位。


void myfunc(){puts(" Hey!Nice bits!"); }


你的意思是补充。或者,正确且更容易拼写,''反转''。

结果就是这样。 11000111

类似的东西。

long myfunc(long var,int pos,int size)
{
long mask = 0;
long tmp;
int i;
Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
void myfunc () { puts ("Hey! Nice bits!"); }

You meant ''complement''. Or, as correct and easier to spell, ''invert''.
The result would thus be. 11000111

Something like that.

long myfunc(long var, int pos, int size)
{
long mask = 0;
long tmp;
int i;



通常最好将无符号类型用于按位值和

操作。

for(i = pos; i> pos-size; i--)
{
tmp = 1<< (i - 1);
mask = mask | tmp;

必须是1L / *或更好1UL,因为我说* /<< (i - 1)如果要计算的

面具中的任何一个不适合整数但是很长(当然只有

)比int更宽。

并且,掩码| = tmp / *或表达式* /更具惯用性。

}

(多)更好的只是/ * mask = * /〜(~0UL<< size)<< (pos-size-1)。

只要大小严格小于unsigned long的宽度;如果

它可能不是,要么添加特殊情况,要么使用<< 1<<(size-1)

(假设它必须> 0 ,这可能是真的在这里),可以优化(?)到〜1UL<<(size-1)。

或者可能替代〜 (~0UL<< size)由预先计算的表格。

返回var ^ mask;

}


It''s generally best to use unsigned types for bitwise values and
operations.
for(i=pos; i>pos-size; i--)
{
tmp = 1 << (i - 1);
mask = mask | tmp;
Must be 1L /* or better 1UL as I said */ << (i - 1) if any of the
masks to be computed don''t fit in int but do in long (which is only
possible of course if long is wider than int).
And, mask |= tmp /* or the expression */ is more idiomatic.
}
(Much) better just /* mask = */ ~(~0UL << size) << (pos-size-1) .
As long as size is strictly less than the width of unsigned long; if
it may not be, either add a special case or use <<1<<(size-1)
(assuming it must be > 0, as is presumably true here) which can be
optimized(?) to ~1UL<<(size-1).

Or possibly substitute ~(~0UL<<size) by a precomputed table.
return var ^ mask;

}

我无法解决这个问题。
你能帮帮忙吗?
I am not able to figure this one out.
Can you help ?




如果这是家庭作业希望我的回复会迟到不要给b $ b帮助,更不用说OP显然已经足够蹩脚,无论如何都没有机会通过任何认真的课程。


- worldnet.att.net上的David.Thompson1



If this was homework hopefully my reply will be late enough not to
help, not to mention that the OP is apparently lame enough to have no
chance of passing any serious course anyway.

- David.Thompson1 at worldnet.att.net


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

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