不用條件陳述式比較兩個數的大小[google_grp] (zz)

來源:互聯網
上載者:User

說明: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.

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.