Bool expression

Discussion in 'C++' started by 2005, Oct 27, 2006.

  1. 2005

    2005 Guest

    Hi,

    The expression below
    bool Full() {return ((mSize==MAXSIZE) ? true : false);}

    My question is if (mSize==MAXSIZE), would it return 1 or 0?

    Of course I ran the code but it conflicts with the spec that I wanted
    to get a second opinion.

    Thanks
     
    2005, Oct 27, 2006
    #1
    1. Advertising

  2. 2005

    Jim Langston Guest

    "2005" <> wrote in message
    news:...
    > Hi,
    >
    > The expression below
    > bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    >
    > My question is if (mSize==MAXSIZE), would it return 1 or 0?
    >
    > Of course I ran the code but it conflicts with the spec that I wanted
    > to get a second opinion.
    >
    > Thanks


    I don't understand your question. If mSize is equal to MAXSIZE the function
    will return true, otherwise it woudl return false, but I don't know why it
    just wasn't coded

    return mSize == MAXSIZE;
    which would do the exact same thing.
     
    Jim Langston, Oct 27, 2006
    #2
    1. Advertising

  3. 2005

    Mark P Guest

    2005 wrote:
    > Hi,
    >
    > The expression below
    > bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    >
    > My question is if (mSize==MAXSIZE), would it return 1 or 0?


    Neither, it returns "true". Of course, it would be simpler to write:

    return mSize == MAXSIZE;

    If for some reason the result were converted to an integral type, than
    it would be 1, not 0.
     
    Mark P, Oct 27, 2006
    #3
  4. 2005

    2005 Guest

    Jim Langston wrote:
    > "2005" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > The expression below
    > > bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    > >
    > > My question is if (mSize==MAXSIZE), would it return 1 or 0?
    > >
    > > Of course I ran the code but it conflicts with the spec that I wanted
    > > to get a second opinion.
    > >
    > > Thanks

    >
    > I don't understand your question. If mSize is equal to MAXSIZE the function
    > will return true, otherwise it woudl return false, but I don't know why it
    > just wasn't coded
    >


    Well, the function above and a spec was given.
    My question is when it returns true, would it be 1 or 0? eg if (Full(
    <true> )) == 1 or 0 ?

    > return mSize == MAXSIZE;
    > which would do the exact same thing.
     
    2005, Oct 27, 2006
    #4
  5. 2005

    Jim Langston Guest

    "2005" <> wrote in message
    news:...
    >
    > Jim Langston wrote:
    >> "2005" <> wrote in message
    >> news:...
    >> > Hi,
    >> >
    >> > The expression below
    >> > bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    >> >
    >> > My question is if (mSize==MAXSIZE), would it return 1 or 0?
    >> >
    >> > Of course I ran the code but it conflicts with the spec that I wanted
    >> > to get a second opinion.
    >> >
    >> > Thanks

    >>
    >> I don't understand your question. If mSize is equal to MAXSIZE the
    >> function
    >> will return true, otherwise it woudl return false, but I don't know why
    >> it
    >> just wasn't coded
    >>

    >
    > Well, the function above and a spec was given.
    > My question is when it returns true, would it be 1 or 0? eg if (Full(
    > <true> )) == 1 or 0 ?
    >
    >> return mSize == MAXSIZE;
    >> which would do the exact same thing.


    It doesn't return either, it returns true, a boolean value. If you cast a
    boolean value to an int, true will become 1 and false will become 0, but
    there is no guarantee that the boolean value itself is stored that way. The
    compiler is free to store it anyway it wants I understand (one bit, a byte,
    a word, whatever).
     
    Jim Langston, Oct 27, 2006
    #5
  6. 2005

    David Harmon Guest

    On 26 Oct 2006 17:45:22 -0700 in comp.lang.c++, "2005"
    <> wrote,
    >The expression below
    > bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    >
    >My question is if (mSize==MAXSIZE), would it return 1 or 0?


    No, it would return true or false.
    By the way, for directness and clarity that should be written as:

    bool Full() {return mSize==MAXSIZE;}
     
    David Harmon, Oct 27, 2006
    #6
  7. 2005

    2005 Guest

    David Harmon wrote:
    > On 26 Oct 2006 17:45:22 -0700 in comp.lang.c++, "2005"
    > <> wrote,
    > >The expression below
    > > bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    > >
    > >My question is if (mSize==MAXSIZE), would it return 1 or 0?

    >
    > No, it would return true or false.
    > By the way, for directness and clarity that should be written as:
    >
    > bool Full() {return mSize==MAXSIZE;}


    My requirement was to return a 0 when mSize==MAXSIZE.

    I coded and did
    int tmp = Ful(); and found tmp was 1 when mSize==MAXSIZE

    Is it contradictory to what you are saying?
     
    2005, Oct 27, 2006
    #7
  8. 2005

    David Harmon Guest

    On 26 Oct 2006 19:33:36 -0700 in comp.lang.c++, "2005"
    <> wrote,
    >> bool Full() {return mSize==MAXSIZE;}

    >
    >My requirement was to return a 0 when mSize==MAXSIZE.


    Well, that is actually the opposite of your original example.
    In that case, make it

    int Full() {return mSize!=MAXSIZE;}

    ^^^ Note change of return type. If you want to return 0 or 1,
    don't call it bool.

    Either way, adding ?: is just obfuscation.
     
    David Harmon, Oct 27, 2006
    #8
  9. 2005

    David Harmon Guest

    On Fri, 27 Oct 2006 02:53:15 GMT in comp.lang.c++, David Harmon
    <> wrote,
    >On 26 Oct 2006 19:33:36 -0700 in comp.lang.c++, "2005"
    ><> wrote,
    >>> bool Full() {return mSize==MAXSIZE;}

    >>
    >>My requirement was to return a 0 when mSize==MAXSIZE.


    Oh, and if you are going to return 0 when the crock is full and 1
    when it is not. then it should be

    int not_full() {return mSize!=MAXSIZE;}
     
    David Harmon, Oct 27, 2006
    #9
  10. 2005

    Tim Slattery Guest

    "2005" <> wrote:

    >Hi,
    >
    >The expression below
    > bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    >
    >My question is if (mSize==MAXSIZE), would it return 1 or 0?


    Neither one. It would return true, which is a boolean value, not an
    integer.

    --
    Tim Slattery
     
    Tim Slattery, Oct 27, 2006
    #10
  11. Jim Langston wrote:
    > "2005" <> wrote in message
    > news:...
    >> Jim Langston wrote:
    >>> "2005" <> wrote in message
    >>> news:...
    >>>> Hi,
    >>>>
    >>>> The expression below
    >>>> bool Full() {return ((mSize==MAXSIZE) ? true : false);}
    >>>>
    >>>> My question is if (mSize==MAXSIZE), would it return 1 or 0?
    >>>>
    >>>> Of course I ran the code but it conflicts with the spec that I wanted
    >>>> to get a second opinion.
    >>>>
    >>>> Thanks
    >>> I don't understand your question. If mSize is equal to MAXSIZE the
    >>> function
    >>> will return true, otherwise it woudl return false, but I don't know why
    >>> it
    >>> just wasn't coded
    >>>

    >> Well, the function above and a spec was given.
    >> My question is when it returns true, would it be 1 or 0? eg if (Full(
    >> <true> )) == 1 or 0 ?
    >>
    >>> return mSize == MAXSIZE;
    >>> which would do the exact same thing.

    >
    > It doesn't return either, it returns true, a boolean value. If you cast a
    > boolean value to an int, true will become 1 and false will become 0, but
    > there is no guarantee that the boolean value itself is stored that way.


    s/cast/convert/
    The difference is significant; (There are no casts in the following
    snippet):

    int i = true;
    int j = false;
    bool k = 0;
    bool l = 42;

    > The compiler is free to store it anyway it wants I understand (one bit, a byte,
    > a word, whatever).



    --
    Clark S. Cox III
     
    Clark S. Cox III, Oct 27, 2006
    #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. Weng Tianxiang
    Replies:
    2
    Views:
    474
    Weng Tianxiang
    Jun 21, 2005
  2. Ignacio Machin

    Re: Convert DataRow content to bool

    Ignacio Machin, Jul 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    379
    Ignacio Machin
    Jul 7, 2003
  3. Eliyahu Goldin

    Re: Convert DataRow content to bool

    Eliyahu Goldin, Jul 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Eliyahu Goldin
    Jul 7, 2003
  4. Georg Mayer
    Replies:
    1
    Views:
    335
    cosine... zero
    Jun 23, 2004
  5. Patrick Olurotimi Ige

    Cannot implicitly convert type 'object' to 'bool' Error

    Patrick Olurotimi Ige, Jan 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    12,221
    Patrick Olurotimi Ige
    Jan 25, 2005
Loading...

Share This Page