按位运算符 [英] bitwise operators
问题描述
我需要编写一个函数来检查x是否为非零值。如果x = = 0则返回0
,否则返回1。我可以使用的开放运算符是〜& ^ | +
<< >>我不允许使用循环。
我的程序到目前为止看起来像:
int isNonZero(int x){
x =(1<< x)& x;
x = ~x;
x = x& 1;
返回x;
}
除以下情况外,所有情况均有效。
测试isNonZero(-2147483648 [0x80000000])失败。
给0 [0x0]。应该是1 [0x1]
如果尝试了我能想到的一切,我完全没有想法。任何一个
有任何想法我如何修复我的代码来处理这个负数?
我还需要编写一个将检查x是否非零仅使用〜
& ^ | +<< >>
到目前为止我的代码是:
int isLess(int x,int y){
int a = 0;
a = x ^ y;
a = y>> a;
a = a + 1;
a =!a;
返回!a;
}
只适用于大约一半的情况(小的非负数)
任何输入都会受到赞赏。
谢谢,
I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.
My program so far looks like:
int isNonZero(int x) {
x = (1 << x) & x;
x = ~x;
x = x & 1;
return x;
}
All cases work except for the below.
Test isNonZero(-2147483648[0x80000000]) failed.
Gives 0[0x0]. Should be 1[0x1]
If tried everything I could think of and I am all out of ideas. Any one
have any idea how I could fix my code to deal with this negative number?
I also need to write one that will check whether x is nonzero using only ~
& ^ | + << >>
My code so far is:
int isLess(int x, int y) {
int a =0;
a = x ^ y;
a = y >> a;
a = a + 1;
a = !a;
return !a;
}
Which only works for about half the cases (on small non-negative numbers)
Any input would be appreciated.
Thanks,
推荐答案
77scrapper77写道:
77scrapper77 wrote:
我需要编写一个函数来检查是否x非零。如果x = = 0则返回0
,否则返回1。我可以使用的开放运算符是〜& ^ | +
<< >>我不允许使用循环。
我的程序到目前为止看起来像:
int isNonZero(int x){
x = (1 << x)& x;
I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.
My program so far looks like:
int isNonZero(int x) {
x = (1 << x) & x;
大多数x值的未定义行为。该标准对此有用了b / b
关于位移的说法:
"如果右操作数的值为负或大于
或等于提升左操作数的位宽,
行为未定义。
此外,=是运算符,它不在您允许的操作员列表中。
就个人而言,我认为只有
$才能在正确的C中实现你被允许使用的b $ b运营商(虽然我已经准备好了b / b
证明是错误的)。
Undefined behaviour for most values of x. The Standard has this to
say about bit-shifting:
"If the value of the right operand is negative or is greater than
or equal to the width in bits of the promoted left operand, the
behavior is undefined."
Also, = is an operator, and it''s not in your list of allowed operators.
Personally, I don''t think it''s possible in correct C with just the
operators that you are allowed to use (although I am ready to be
proved wrong).
77scrapper77写道:
77scrapper77 wrote:
我需要编写一个函数来检查x是否为非零。如果x = = 0则返回0
,否则返回1。我可以使用的开放运算符是〜& ^ | +
<< >>我不允许使用循环。
我的程序到目前为止看起来像:
int isNonZero(int x){
此函数名称侵入实现的命名空间
,因为它以是开头。制作IsNonZero()。
x =(1<< x)& X;
未定义的行为,请参阅infobahn的消息。
x = ~x;
x = x& 1;
返回x;
}
所有案例除下述情况外均有效。
Nope:x = 0 ...(〜((1 << 0)& 0))& 1 == 1.
所以你最重要的案例不起作用。
我根本看不到你如何做任何明智的签名
整数,因为它们可能有1为零的补零和
符号幅度表示。似乎是一个完全没有希望的
任务。即使只有2s补充,我也没有最少的线索
怎么做。
测试isNonZero(-2147483648 [0x80000000])失败。
Gives 0为0x0]。应该是1 [0x1]
如果尝试了我能想到的一切,我完全没有想法。任何人都知道如何修复我的代码来处理这个负数?
编号完全没有。
我还需要写一个只用〜
来检查x是否非零的
ITYM是否x< y
& ^ | +<< >>
到目前为止,我的代码是:
int isLess(int x,int y){
再次:函数名称。
int a = 0;
a = x ^ y;
a = y>>一个;
您似乎完全不知道班次操作员在做什么。
a = a + 1;
a =!a;
返回!a;
!也是一个操作员,而不是你的清单。
如果不是,你的IsNonZero()函数的主体将包含
{return !! x ;}
}
只适用于大约一半的情况(小的非负数)
任何输入都会受到赞赏。
I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.
My program so far looks like:
int isNonZero(int x) { This function name invades the implementation''s namespace
as it starts with "is". Make that IsNonZero().
x = (1 << x) & x;
Undefined behaviour, see infobahn''s message.
x = ~x;
x = x & 1;
return x;
}
All cases work except for the below.
Nope: x=0 ... (~((1<<0) & 0)) & 1 == 1.
So your most important case does not work.
I do not see at all how you can do anything sensible for signed
integers as they could have negative zeros for 1s complement and
sign-magnitude representations. Seems to be a completely hopeless
task. Even for 2s complement only, I do not have the least clue
how to do it.
Test isNonZero(-2147483648[0x80000000]) failed.
Gives 0[0x0]. Should be 1[0x1]
If tried everything I could think of and I am all out of ideas. Any one
have any idea how I could fix my code to deal with this negative number?
No. Not at all.
I also need to write one that will check whether x is nonzero using only ~
ITYM whether x<y
& ^ | + << >>
My code so far is:
int isLess(int x, int y) {
Again: The function name.
int a =0;
a = x ^ y;
a = y >> a;
You seem to be completely clueless what the shift operators are doing.
a = a + 1;
a = !a;
return !a;
! is an operator, too, and not on your list.
If it was not, your IsNonZero() function''s body would consist of
{return !!x;}
}
Which only works for about half the cases (on small non-negative numbers)
Any input would be appreciated.
你是否可以使用if,else,...?
要么你没有告诉我们全部真相,你应该看看
给不同的人给你C问题。
干杯
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
Are you allowed to use if, else, ...?
Either you are not telling us the whole truth or you should look
for someone different to give you C problems.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
" 77scrapper77" < GR *************** @ nospam.hotmail.com>写道:
"77scrapper77" <gr***************@nospam.hotmail.com> writes:
我需要编写一个函数来检查x是否为非零。如果x = = 0则返回0
,否则返回1。我可以使用的开放运算符是〜& ^ | +
<< >>我不允许使用循环。
I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.
int is_nonzero(int x)
{
switch (x){
案例0:
返回0;
默认值:
返回1;
}
}
-
有些编程实践会产生错误;
这个就像拨打一个800号码
并将错误发送到你的门口。
- Steve McConnell
int is_nonzero (int x)
{
switch (x) {
case 0:
return 0;
default:
return 1;
}
}
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
这篇关于按位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!