Bool expression

2

2005

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
 
J

Jim Langston

2005 said:
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.
 
M

Mark P

2005 said:
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.
 
2

2005

Jim said:
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(
 
J

Jim Langston

2005 said:
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(

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

David Harmon

On 26 Oct 2006 17:45:22 -0700 in comp.lang.c++, "2005"
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;}
 
2

2005

David said:
On 26 Oct 2006 17:45:22 -0700 in comp.lang.c++, "2005"


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

David Harmon

On 26 Oct 2006 19:33:36 -0700 in comp.lang.c++, "2005"
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.
 
D

David Harmon

On Fri, 27 Oct 2006 02:53:15 GMT in comp.lang.c++, David Harmon
On 26 Oct 2006 19:33:36 -0700 in comp.lang.c++, "2005"

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;}
 
T

Tim Slattery

2005 said:
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
(e-mail address removed)
 
C

Clark S. Cox III

Jim said:
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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top