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