我怎么能比较明智? [英] How can i compare bit wise?

查看:112
本文介绍了我怎么能比较明智?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想写一个程序,找到两个数字中较大的一个..

当然条件很小......不使用如果"和有条件的

运营商......我对如何实施该程序有了一个想法......我还有一个

的替代方案......那就是使用按位比较方式:

方式:

接受输入

从另一个号码中减去一个号码

检查MSB(结果中最重要的一点......

看看结果是正面还是负面......

如果为负则则第二个数字更大...


虽然我能想到算法,但我无法用简单的方式编写代码,这可以检查结果的最高位...

如何做到这一点......?


顺便说一句,这是我用于简单解决方案的逻辑:

/ *通常的C语句* /

max = a + b + abs(ab);

/ *通常手续后的计划结束* /


我只是想知道如何测试一个单位......是否是

正面还是负面,即0或1 ...


最后,有没有什么方法可以直接在其二进制

表格中打印一个整数,而无需为此编写程序?

解决方案

糟糕...得到了如何检查最后一位的答案..


x = number>> ;(sizeof(int)* 8-1);


然后检查x是0还是非零...


但是第二部分对我来说仍然是一个难题......有没有什么方法我可以直接打印二进制形式的整数?


< BLOCKQUOTE>"净化" < CN ****** @ gmail.com>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

糟糕...得到了如何检查最后一位的答案..

x =数字>>(sizeof(int)* 8-1);

然后检查x是0还是非零...

然而第二部分对我来说仍然是一个难题...有没有办法我可以打印二进制表格一个整数直接?




没有内置设施或功能,但是写一个并不难。事实上,如果你搜索clc的档案,你应该

在我和其他人的代码中找到代码

发布在这里在过去。


-Mike




Mike Wahler写道:

" purifier" < CN ****** @ gmail.com>在消息中写道
新闻:11 ********************** @ c13g2000cwb.googlegr oups.com ...

糟糕...得到了如何检查最后一位的答案..

x =数字>>(sizeof(int)* 8-1);

然后检查x是0还是非零...

然而第二部分对我来说仍然是一个难题...有没有什么方法
i可以直接打印二进制形式的整数?



没有内置设施或功能,但写一个并不困难。事实上,如果您搜索clc的档案,您应该在我和其他人过去发布的代码中找到代码示例。

-Mike




谢谢Mike ......

实际上我自己写了一个程序......但是我想知道C

已经有内置功能......无论如何,谢谢

澄清......


Hi everyone,
I thought of writing a program which finds the greater of two numbers..
of course with a small condition... without using "if" and conditional
operators... I got an idea on how to implement the program... I have an
alternative too... that is to use bitwise comparison in the folowing
manner:
Accept the input
Subtract one number from the other
Check for the MSB(Most Significant Bit) in the result...
See if the result is positive or negative...
If negative then the second number is bigger...

Though i could think of the algorithm, i am unable to write a code in a
simple manner which can check the Most significant bit of the result...
How can do it...?

By the way, this was the logic i used for a simple solution:

/*The usual C statements*/
max=a+b+abs(a-b);
/*End of Program after the usual formalities*/

I just wanted to know how to test a single bit... whether it is
positive or negative i.e. 0 or 1...

And finally, is there any way i could print an integer in its binary
form directly without writing a program for that?

解决方案

Oops...got the answer on how to check for the last bit..

x=number>>(sizeof(int)*8-1);

and then checking if x is 0 or non zero...

However the second part remains a puzzle to me... Is there any way i
can print binary form of an integer directly?


"purifier" <cn******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Oops...got the answer on how to check for the last bit..

x=number>>(sizeof(int)*8-1);

and then checking if x is 0 or non zero...

However the second part remains a puzzle to me... Is there any way i
can print binary form of an integer directly?



There is no built-in facility or function, but
writing one is not difficult. As a matter of
fact, if you search the archives of clc you should
find examples in code that I and others have
posted here in the past.

-Mike



Mike Wahler wrote:

"purifier" <cn******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Oops...got the answer on how to check for the last bit..

x=number>>(sizeof(int)*8-1);

and then checking if x is 0 or non zero...

However the second part remains a puzzle to me... Is there any way i can print binary form of an integer directly?



There is no built-in facility or function, but
writing one is not difficult. As a matter of
fact, if you search the archives of clc you should
find examples in code that I and others have
posted here in the past.

-Mike



Thanks Mike...
Actually i did write a program myself... but i was wondering if C
already had a built in function... anyway thanks for the
clarification...


这篇关于我怎么能比较明智?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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