Re: How can i compare bit wise?

Discussion in 'C Programming' started by purifier, Jan 19, 2005.

1. purifierGuest

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, Jan 19, 2005

2. Mike WahlerGuest

"purifier" <> wrote in message
news:...
> 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, Jan 19, 2005

3. purifierGuest

Mike Wahler wrote:
> "purifier" <> wrote in message
> news:...
> > 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
clarification...

purifier, Jan 19, 2005
4. Keith ThompsonGuest

"purifier" <> 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.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Jan 19, 2005
5. Old WolfGuest

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.

Old Wolf, Jan 19, 2005
6. JerryGuest

/*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^_~*/

Jerry, Jan 20, 2005
7. sushantGuest

u can also mask the bits appropriately using the logical operators

sushant, Jan 20, 2005
8. sushantGuest

u can also mask the bits appropriately using the logical operators

sushant, Jan 20, 2005
9. CBFalconerGuest

The Google Problem (was: How can i compare bit wise?)

sushant wrote:
>
> u can also mask the bits appropriately using the logical operators

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
sequence. So lets have a massive campaign to improve Google.
Write to:

Groups Beta Support <>

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

CBFalconer, Jan 20, 2005
10. Old WolfGuest

Re: The Google Problem (was: How can i compare bit wise?)

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
> sequence. So lets have a massive campaign to improve Google.
> Write to:
>
> Groups Beta Support <>

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

Actually this is exceptionally dumb on google's part. The

Old Wolf, Jan 20, 2005
11. Default UserGuest

Re: The Google Problem (was: How can i compare bit wise?)

CBFalconer wrote:

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

Indeed. I've posted Help messages in a few groups (it's already been
discussed here).

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

It's not all *that* difficult. I've been using Google all year to post
because our outgoing feed in broken, although I can read perfectly
well.

> sequence. So lets have a massive campaign to improve Google.
> Write to:
>
> Groups Beta Support <>

Might as well, but I don't hold out much hope. I know they've heard

They also need to fix the space contraction that messes up code
fragment, and the inconsistent policy of conflating multiple blank
lines.

Brian

Default User, Jan 20, 2005