Re: Comparing with 0.

From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 02/20/05

  • Next message: David White: "Re: Comparing with 0."
    Date: Sun, 20 Feb 2005 22:12:18 GMT
    
    

    Val wrote:
    > How does C++ compare an integer with the value "0"?
    >
    > I've tried to fiddle with the biswise operators but I can't do it.
    > Is the "==" operator in C++ somewhere defined in a header or source file for
    > (say) integers? Where?
    > Obviously I would like to write (myInt == 0) in terms of bitwise operators
    > for an 8 bit=1 byte (32 bit) machine.
    >
    >

    I don't understand your issue. Please clarify.
    The language supports the equality operator for
    signed and unsigned integers of all lengths.

    If you want to compare whether a set of bits
    within an unsigned integer are zero, then
    look up "masking" in your text book.

    The steps are:
    1. Create a "mask" containing the bit values
        for each bit you want to test.
        Example: 0xFF represents the "lowest" 8 bits.

    2. Arithmetic AND the unsigned integer with the
        mask:
           result = value & 0xFF;

    3. Test for zero.
           if ((value & 0xff) == 0) /*... */

    The truth table for AND is:
       A B Result (A AND B)
      --- --- ------
       0 0 0
       0 1 0
       1 0 0
       1 1 1
    Commit this to memory if you are going to be
    bit-twiddling.

    -- 
    Thomas Matthews
    C++ newsgroup welcome message:
              http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
              http://www.comeaucomputing.com/learn/faq/
    Other sites:
         http://www.josuttis.com  -- C++ STL Library book
         http://www.sgi.com/tech/stl -- Standard Template Library
    

  • Next message: David White: "Re: Comparing with 0."

    Relevant Pages