不用条件语句比较两个数的大小[google_grp] (zz)

说明:google group里面有不少技术讨论是很不错的。zz from http://blog.donews.com/zinany/archive/2005/01/20/248393.aspx

purifier  Jan 19, 8:25 am     show options

Newsgroups: comp.lang.c
From: "purifier" <cnbct...@gmail.com> - Find messages by this author
Date: 19 Jan 2005 08:25:53 -0800
Local: Wed, Jan 19 2005 8:25 am
Subject: How can i compare bit wise?
Reply | Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse

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...
/*I write a simple routine just now*/
void int2bin(int ori)
{
int mask=0x01;
int len=sizeof(int)*8-1;
printf("%d",len);
while(len>-1)
{
printf("%d",(ori&(mask<<len--))?1:0);
        }

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?
--------------------------------------------------------------

/*here below I wrote a routine to solve the problem*/

/*blow by zinan.y*/

 

}

/*well,anyhow,It works^_~*/

------------------------------

/*below by old wolf,a nice idea*/

You point to it with an (unsigned char *), and then
printf with "%02x" for each byte (if you want hex output).

Actually I have a similar problem to yours in some real code.
The poxy compiler I have to use, issues a compile-time warning
if a conditional is always true or always false, and there
is no way to turn that specific warning off. So I have
a macro:

#define IS_LT(x,y) (!!( ((ulong)(x) - (y)) & 0x8000000UL ))

that is non-zero if x < y . This macro does rely on some things
specific to my implementation (eg. size of ulong, and defined
behaviour on int overflow, in case x and y are signed int/long).
I don't entirely trust this macro; I only use it for
STATIC_ASSERTs.

 

 

}

/*well,anyhow,It works^_~*/

------------------------------

/*below by old wolf,a nice idea*/

You point to it with an (unsigned char *), and then
printf with "%02x" for each byte (if you want hex output).

Actually I have a similar problem to yours in some real code.
The poxy compiler I have to use, issues a compile-time warning
if a conditional is always true or always false, and there
is no way to turn that specific warning off. So I have
a macro:

#define IS_LT(x,y) (!!( ((ulong)(x) - (y)) & 0x8000000UL ))

that is non-zero if x < y . This macro does rely on some things
specific to my implementation (eg. size of ulong, and defined
behaviour on int overflow, in case x and y are signed int/long).
I don't entirely trust this macro; I only use it for
STATIC_ASSERTs.

 

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?
--------------------------------------------------------------

/*here below I wrote a routine to solve the problem*/

/*blow by zinan.y*/

/*I write a simple routine just now*/
void int2bin(int ori)
{
int mask=0x01;
int len=sizeof(int)*8-1;
printf("%d",len);
while(len>-1)
{
printf("%d",(ori&(mask<<len--))?1:0);
        }

}

/*well,anyhow,It works^_~*/

------------------------------

/*below by old wolf,a nice idea*/

You point to it with an (unsigned char *), and then
printf with "%02x" for each byte (if you want hex output).

Actually I have a similar problem to yours in some real code.
The poxy compiler I have to use, issues a compile-time warning
if a conditional is always true or always false, and there
is no way to turn that specific warning off. So I have
a macro:

#define IS_LT(x,y) (!!( ((ulong)(x) - (y)) & 0x8000000UL ))

that is non-zero if x < y . This macro does rely on some things
specific to my implementation (eg. size of ulong, and defined
behaviour on int overflow, in case x and y are signed int/long).
I don't entirely trust this macro; I only use it for
STATIC_ASSERTs.

 

 

}

/*well,anyhow,It works^_~*/

------------------------------

/*below by old wolf,a nice idea*/

You point to it with an (unsigned char *), and then
printf with "%02x" for each byte (if you want hex output).

Actually I have a similar problem to yours in some real code.
The poxy compiler I have to use, issues a compile-time warning
if a conditional is always true or always false, and there
is no way to turn that specific warning off. So I have
a macro:

#define IS_LT(x,y) (!!( ((ulong)(x) - (y)) & 0x8000000UL ))

that is non-zero if x < y . This macro does rely on some things
specific to my implementation (eg. size of ulong, and defined
behaviour on int overflow, in case x and y are signed int/long).
I don't entirely trust this macro; I only use it for
STATIC_ASSERTs.

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值