Re: How can i compare bit wise?

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

  1. purifier

    purifier Guest

    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
    #1
    1. Advertising

  2. purifier

    Mike Wahler Guest

    "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
    #2
    1. Advertising

  3. purifier

    purifier Guest

    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
    already had a built in function... anyway thanks for the
    clarification...
    purifier, Jan 19, 2005
    #3
  4. "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
    #4
  5. purifier

    Old Wolf Guest

    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
    #5
  6. purifier

    Jerry Guest

    /*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^_~*/
    Jerry, Jan 20, 2005
    #6
  7. purifier

    sushant Guest

    u can also mask the bits appropriately using the logical operators
    instead of shifting ......
    sushant, Jan 20, 2005
    #7
  8. purifier

    sushant Guest

    u can also mask the bits appropriately using the logical operators
    instead of shifting ......
    sushant, Jan 20, 2005
    #8
  9. purifier

    CBFalconer Guest

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

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

    --
    "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
    CBFalconer, Jan 20, 2005
    #9
  10. purifier

    Old Wolf Guest

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


    You may have to also do a bit of trickery to stop your
    email addresses being masked off..

    Actually this is exceptionally dumb on google's part. The
    email addresses appear masked when you read, but if you
    Reply with quoting, then all the addresses appear unmasked!
    Old Wolf, Jan 20, 2005
    #10
  11. purifier

    Default User Guest

    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.

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


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

    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
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrinalroy

    Bit wise shift operators

    mrinalroy, Oct 20, 2006, in forum: Java
    Replies:
    3
    Views:
    434
    IchBin
    Oct 20, 2006
  2. Jack Klein

    Re: How can i compare bit wise?

    Jack Klein, Jan 20, 2005, in forum: C Programming
    Replies:
    1
    Views:
    467
  3. s.subbarayan
    Replies:
    4
    Views:
    363
    s.subbarayan
    Apr 7, 2005
  4. Enrique Cruiz

    Row-wise vs. column-wise image processing

    Enrique Cruiz, Jan 25, 2007, in forum: C Programming
    Replies:
    10
    Views:
    717
    christian.bau
    Jan 26, 2007
  5. Enrique Cruiz
    Replies:
    5
    Views:
    415
    Jim Langston
    Jan 25, 2007
Loading...

Share This Page