有点操纵 [英] Some bit manipulation
问题描述
我正在寻找以下内容:
我们有两个32位整数A和B.我需要提出一个操作
O以A和B为参数,吐出单个整数R,如果A中位置b的位置位,那么R中位置b的位>
被清除;否则,B中位置b的位的值被复制到R.
这将对参数中的所有32位进行。
I am looking for some bit wizardy for the following:
We have two 32-bit integers A and B. I need to come up with an operation
O that takes A and B as arguments, spitting out a single integer R such
that if the bit in position b in A is set, then the bit in position b in R
is cleared; otherwise, the value of the bit in position b in B is copied to R.
This is to be done for all the 32 bits in the arguments.
推荐答案
Henry Samuelson写道:
Henry Samuelson wrote:
我正在为以下内容寻找一些内容:
我们有两个32-位整数A和B.我需要提出一个操作
O,它将A和B作为参数,吐出一个整数R,如果A中的位置b被设置,那么R
中位置b的位被清除;否则,B中位置b的位的值被复制到R.
这将对参数中的所有32位进行。
I am looking for some bit wizardy for the following:
We have two 32-bit integers A and B. I need to come up with an operation
O that takes A and B as arguments, spitting out a single integer R such
that if the bit in position b in A is set, then the bit in position b in R
is cleared; otherwise, the value of the bit in position b in B is copied to R.
This is to be done for all the 32 bits in the arguments.
你的问题... b(位置)来自哪里?
-
Sean
Question for you...Where does b (position) come from?
--
Sean
>
Henry Samuelson写道:
Henry Samuelson wrote:
我正在寻找以下内容:
我们有两个32位整数A和B.我需要来使用A和B作为参数的O操作,将一个整数R吐出,如果A中的位置b中的位被设置,那么R中位置b的位/>被清除;否则,B中位置b的位的值被复制到R.
这将对参数中的所有32位进行。
I am looking for some bit wizardy for the following:
We have two 32-bit integers A and B. I need to come up with an operation
O that takes A and B as arguments, spitting out a single integer R such
that if the bit in position b in A is set, then the bit in position b in R
is cleared; otherwise, the value of the bit in position b in B is copied to R.
This is to be done for all the 32 bits in the arguments.
R = B& ~A;
作为一项规则,你应该使用一些'unsigned''
整数来进行bit-fiddling,因为它可以避免潜在的当符号位参与时,
令人不快的惊喜。
-
Er ********* @ sun.com
R = B & ~A;
As a rule, you should be using some flavor of `unsigned''
integers for bit-fiddling, because it avoids potentially
unpleasant surprises when sign bits participate.
--
Er*********@sun.com
Henry Samuelson写道:
Henry Samuelson wrote:
我们有两个32位整数A和B.我需要提出一个操作
O,它将A和B作为参数,吐出一个单位整数R,如果该位设置A中的位置b,然后清除R
中位置b的位;否则,B中位置b的位的值被复制到R.
We have two 32-bit integers A and B. I need to come up with an operation
O that takes A and B as arguments, spitting out a single integer R such
that if the bit in position b in A is set, then the bit in position b in R
is cleared; otherwise, the value of the bit in position b in B is copied to R.
我将真诚地假设这不是一个家庭作业的问题。要从任何特定的逻辑函数中取出
,有助于查看真值表:
BA = 0 A = 1
0 0 0
1 1 0
如你所见,只有当A'位清零且B'位<时才设置R中的位br />
已设定。换句话说,如果设置了~A'位并设置了B'位,或者:
R = ~A& B;
-
Derrick Coetzee
我将此新闻组发布到公共领域。我不承担所有
明示或暗示保证并承担所有责任。我不是专业人士。
I''m going to assume in good faith this isn''t a homework problem. To pull
off any particular logical function, it helps to look at the truth table:
B A=0 A=1
0 0 0
1 1 0
As you can see, the bit in R is set only if A''s bit is clear AND B''s bit
is set. In other words, if ~A''s bit is set and B''s bit is set, or:
R = ~A & B;
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
这篇关于有点操纵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!