Velocity Reviews > Re: How can i compare bit wise?

# Re: How can i compare bit wise?

purifier
Guest
Posts: n/a

 01-19-2005
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?

Mike Wahler
Guest
Posts: n/a

 01-19-2005
"purifier" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

purifier
Guest
Posts: n/a

 01-19-2005

Mike Wahler wrote:
> "purifier" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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...

Keith Thompson
Guest
Posts: n/a

 01-19-2005
"purifier" <(E-Mail Removed)> writes:
> 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...

What is the significance of the number 8? (Hint: CHAR_BIT, the number
of bits in a byte, isn't necessarily 8.)

If number is negative, the result of a right-shift operator is
implementation-defined.

You're also assuming that int has no padding bits (i.e., that you can
determine the number of significant bits from sizeof(int)). That's
true on most systems, but it's not guaranteed.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Old Wolf
Guest
Posts: n/a

 01-19-2005
purifier wrote:

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

That only works if 'number' is an unsigned int .
Right-shifts of a signed value are implementation-defined
as to whether the new bits are 0 or 1.

You could improve it slightly:

.. x = 1UL & (number >> sizeof number * CHAR_BIT - 1);

then it will work for any integral type (except for long long).

BTW, checking for the MSB does not necessarily indicate
positive or negative (although MSB = sign-bit is a very
common implementation). The easiest way to check if
x is negative is: (x < 0)

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

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.

Jerry
Guest
Posts: n/a

 01-20-2005
/*I write a simple routine just now*/
void int2bin(int ori)
{
int len=sizeof(int)*8-1;
printf("%d",len);
while(len>-1)
{
}
}
/*well,anyhow,It works^_~*/

sushant
Guest
Posts: n/a

 01-20-2005
u can also mask the bits appropriately using the logical operators
instead of shifting ......

sushant
Guest
Posts: n/a

 01-20-2005
u can also mask the bits appropriately using the logical operators
instead of shifting ......

CBFalconer
Guest
Posts: n/a

 01-20-2005
sushant wrote:
>
> u can also mask the bits appropriately using the logical operators
> instead of shifting ......

The presence of the flawed google usenet interface is flooding
newsgroups with ignorant posters (such as sushant), who have no
idea of the format of a suitable message (attributed and snipped
quotes, bottom-posted). At the same time that interface seems to
be introducing many to the power of usenet, which is a good thing.

So the best hope is to persuade Google to alter their foolish and
incompetent interface. I can easily see why neophytes choose the
'reply' button on the end of a message, rather than laboriously
scrolling upwards to find the 'show options' button. The answer is
dirt simple - make the reply button do the showoptions/reply
sequence. So lets have a massive campaign to improve Google.
Write to:

Groups Beta Support <(E-Mail Removed)>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Old Wolf
Guest
Posts: n/a

 01-20-2005
CBFalconer wrote:
>
> So the best hope is to persuade Google to alter their foolish and
> incompetent interface. I can easily see why neophytes choose the
> 'reply' button on the end of a message, rather than laboriously
> scrolling upwards to find the 'show options' button. The answer is
> dirt simple - make the reply button do the showoptions/reply
> sequence. So lets have a massive campaign to improve Google.
> Write to:
>
> Groups Beta Support <(E-Mail Removed)>

You may have to also do a bit of trickery to stop your

Actually this is exceptionally dumb on google's part. The
email addresses appear masked when you read, but if you