Does true ^ true return false?

Discussion in 'C++' started by Siemel Naran, Jun 17, 2004.

  1. Siemel Naran

    Siemel Naran Guest

    Does true ^ true return false?

    int silly() { return 3; }

    int main() {
    bool t1 = silly();
    bool t2 = true;
    cout << (t1 ^ t2) << '\n';
    }

    Does the above program print "0" because true^true is false.

    Or does it print "1" because 00000011^00000001 = 00000010
     
    Siemel Naran, Jun 17, 2004
    #1
    1. Advertising

  2. "Siemel Naran" <> wrote in message
    news:8%aAc.82249$...
    > Does true ^ true return false?
    >
    > int silly() { return 3; }
    >
    > int main() {
    > bool t1 = silly();
    > bool t2 = true;
    > cout << (t1 ^ t2) << '\n';
    > }
    >
    > Does the above program print "0" because true^true is false.
    >
    > Or does it print "1" because 00000011^00000001 = 00000010
    >


    Surely it would take you five minutes to find out yourself? The answer is
    "0". When 3 has been converted to a bool, it forgets it was ever 3 and just
    becomes true.

    john
     
    John Harrison, Jun 17, 2004
    #2
    1. Advertising

  3. Siemel Naran

    AngleWyrm Guest

    "Siemel Naran" <> wrote in message
    news:8%aAc.82249$...
    > Does true ^ true return false?
    >
    > int silly() { return 3; }
    >
    > int main() {
    > bool t1 = silly();
    > bool t2 = true;
    > cout << (t1 ^ t2) << '\n';

    cout << "does t1 == 3? " << (t1 == 3) << " or not? " << (t1!=3) <<
    endl;
    > }
    >
    > Does the above program print "0" because true^true is false.
    >
    > Or does it print "1" because 00000011^00000001 = 00000010


    It prints 0 because (true XOR true) is false.
    t1 != 00000011.
     
    AngleWyrm, Jun 17, 2004
    #3
  4. Siemel Naran

    Sharad Kala Guest

    "John Harrison" <> wrote in message
    news:...
    >
    >
    > Surely it would take you five minutes to find out yourself? The answer is
    > "0". When 3 has been converted to a bool, it forgets it was ever 3 and just
    > becomes true.


    Yes, and i think it's clear from the standard.
    4.12 An rvalue of arithmetic, enumeration, pointer, or pointer to member type
    can be converted to an rvalue of type bool. A zero value, null pointer value, or
    null member pointer value is converted to false; any other value is converted to
    true.

    -Sharad
     
    Sharad Kala, Jun 17, 2004
    #4
  5. Siemel Naran

    Siemel Naran Guest

    "John Harrison" <> wrote in message
    news:2jct25Fuvoj4U1@uni-

    > Surely it would take you five minutes to find out yourself? The answer is
    > "0". When 3 has been converted to a bool, it forgets it was ever 3 and

    just
    > becomes true.


    I had a hunch what the right answer was, but just wanted to be sure about
    the matter, because my code will have to run correctly on many different
    platforms from UNIX to Windows. As for the standard and its quotes, I find
    it very hard to understand in these matters of bit operations and such.
     
    Siemel Naran, Jun 17, 2004
    #5
  6. Siemel Naran

    AngleWyrm Guest

    "Siemel Naran" <> wrote in message
    news:6rcAc.82674$...
    >
    > I had a hunch what the right answer was, but just wanted to be sure about
    > the matter, because my code will have to run correctly on many different
    > platforms from UNIX to Windows. As for the standard and its quotes, I

    find
    > it very hard to understand in these matters of bit operations and such.
    >


    #include <cstdlib>
    #include <iostream>
    using namespace std;

    int silly() { return 3; }

    int main() {
    bool t1 = silly(); // converts an int to a bool

    // it's the same as saying t1 = (bool)( silly() )
    cout << "is bool(silly()) same? "
    << ( t1 == (bool)(silly()) ) << endl;

    // but just like converting a float to an int, resolution is lost
    // converting bool back to int results in 1 or 0
    cout << "is t1 == 1 true? " << ( t1==1 )
    << " or is it false? " << (t1 != 1)
    << endl;

    // exclusive or is only one or the other, but not both:
    cout << "\n1^0=" << (true^false) << "\n0^1=" << (false^true)
    << "\n1^1=" << (true^true) << "\n0^0=" << (false^false)
    << endl;

    system("pause");
    }
     
    AngleWyrm, Jun 17, 2004
    #6
  7. Siemel Naran

    Rolf Magnus Guest

    Siemel Naran wrote:

    > "John Harrison" <> wrote in message
    > news:2jct25Fuvoj4U1@uni-
    >
    >> Surely it would take you five minutes to find out yourself? The
    >> answer is "0". When 3 has been converted to a bool, it forgets it was
    >> ever 3 and

    > just
    >> becomes true.

    >
    > I had a hunch what the right answer was, but just wanted to be sure
    > about the matter, because my code will have to run correctly on many
    > different platforms from UNIX to Windows. As for the standard and its
    > quotes, I find it very hard to understand in these matters of bit
    > operations and such.


    What is hard about that? A bool is a bool is a bool. There are only two
    possible values for bool, which are true and false. It doesn't matter
    what you used to initialize your variable. Bit operations don't have
    anything to do with that.
     
    Rolf Magnus, Jun 17, 2004
    #7
  8. Siemel Naran

    AngleWyrm Guest

    An interesting use of xor is for switching. If we have a bool variable, and
    we xor it with 0, what happens?
    0 ^ 0 = 0
    1 ^ 0 = 1
    The result is the same as the original before we xor'd it. But if we xor it
    with a 1:
    0 ^ 1 = 1
    1 ^ 1 = 0
    The result is exactly opposite of the original. So we can use this to
    either leave a bit alone, or switch it over to the other state. It becomes
    a way to flip a switch, or change a flag or state.

    The other functions, & and | do things like this as well. With AND, if we
    use a 1:
    0 & 1 = 0
    1 & 1 = 1
    The original state is preserved, but if we use a 0:
    0 & 0 = 0
    1 & 0 = 0
    The result is always 0. It can be used to turn something off, or set it to
    zero, or even be thought of as allowing/disallowing the state of the bit to
    show through.

    With OR, if we use a 0:
    0 | 0 = 0
    1 | 0 = 1
    The original is once again showing through normally. But if we use a 1:
    0 | 1 = 1
    1 | 1 = 1
    The result is always on, or set to 1.
     
    AngleWyrm, Jun 17, 2004
    #8
  9. Siemel Naran

    David Harmon Guest

    On Thu, 17 Jun 2004 08:16:02 GMT in comp.lang.c++, "Siemel Naran"
    <> wrote,
    >platforms from UNIX to Windows. As for the standard and its quotes, I find
    >it very hard to understand in these matters of bit operations and such.


    It is probably a mistake to use ^ bitwise exclusive or with bool
    operands. Use != instead.
     
    David Harmon, Jun 17, 2004
    #9
  10. Siemel Naran

    Old Wolf Guest

    "AngleWyrm" <> wrote:
    > "Siemel Naran" <> wrote:
    >
    > > int silly() { return 3; }
    > >
    > > int main() {
    > > bool t1 = silly();
    > > bool t2 = true;
    > > cout << (t1 ^ t2) << '\n';

    > cout << "does t1 == 3? " << (t1 == 3) << " or not? " << (t1!=3) <<
    > endl;
    > > }
    > >
    > > Does the above program print "0" because true^true is false.
    > >
    > > Or does it print "1" because 00000011^00000001 = 00000010

    >
    > It prints 0 because (true XOR true) is false.


    Actually, (true ^ true) is 0 because the operands of '^' undergo
    integer promotions. If 0 is assigned to a bool then it would
    be converted to 'false', but the OP's code didn't do that.

    If he changed his first cout line to:
    cout << boolalpha << (t1 ^ t2) << ' ' << t1 << '\n'
    it would print "0 true", not "false true".
     
    Old Wolf, Jun 17, 2004
    #10
  11. Siemel Naran

    Chris Theis Guest

    "Siemel Naran" <> schrieb im Newsbeitrag
    news:6rcAc.82674$...
    > "John Harrison" <> wrote in message
    > news:2jct25Fuvoj4U1@uni-
    >
    > > Surely it would take you five minutes to find out yourself? The answer

    is
    > > "0". When 3 has been converted to a bool, it forgets it was ever 3 and

    > just
    > > becomes true.

    >
    > I had a hunch what the right answer was, but just wanted to be sure about
    > the matter, because my code will have to run correctly on many different
    > platforms from UNIX to Windows. As for the standard and its quotes, I

    find
    > it very hard to understand in these matters of bit operations and such.


    This is rather a question of conversion but really bit ops.

    Regards
    Chris
     
    Chris Theis, Jun 17, 2004
    #11
  12. Siemel Naran

    Chris Theis Guest

    "David Harmon" <> schrieb im Newsbeitrag
    news:...
    > On Thu, 17 Jun 2004 08:16:02 GMT in comp.lang.c++, "Siemel Naran"
    > <> wrote,
    > >platforms from UNIX to Windows. As for the standard and its quotes, I

    find
    > >it very hard to understand in these matters of bit operations and such.

    >
    > It is probably a mistake to use ^ bitwise exclusive or with bool
    > operands. Use != instead.
    >


    Why do you consider using ^ bitwise exclusive or with bool operads a
    mistake? IMHO this is perfectly okay and I can't see why one should refrain
    (however I might be overlooking something).

    Regards
    Chris
     
    Chris Theis, Jun 17, 2004
    #12
  13. Siemel Naran

    David Harmon Guest

    On Thu, 17 Jun 2004 23:11:04 +0200 in comp.lang.c++, "Chris Theis" <> wrote,
    >Why do you consider using ^ bitwise exclusive or with bool operads a
    >mistake? IMHO this is perfectly okay and I can't see why one should refrain
    >(however I might be overlooking something).


    Convert from bool to int, then bitwise xor. How is that helpful?

    Assuming you wanted a bool result, != is very clear as to what is intended and could have avoided this whole thread.
     
    David Harmon, Jun 18, 2004
    #13
  14. Siemel Naran

    Eric Schmidt Guest

    "Siemel Naran" <> wrote in message news:<8%aAc.82249$>...
    > Does true ^ true return false?
    >
    > int silly() { return 3; }
    >
    > int main() {
    > bool t1 = silly();
    > bool t2 = true;
    > cout << (t1 ^ t2) << '\n';
    > }
    >
    > Does the above program print "0" because true^true is false.
    >
    > Or does it print "1" because 00000011^00000001 = 00000010


    The above program is ill-formed because it uses an undeclared
    identifier cout. Therefore, if it even compiles, it may print
    anything.

    --
    Eric Schmidt "Eric119"
    This statement is not necessarily true.
     
    Eric Schmidt, Jun 18, 2004
    #14
  15. Siemel Naran

    Mats Weber Guest

    In article <cat1d8$8ca$>,
    "Chris Theis" <> wrote:

    >Why do you consider using ^ bitwise exclusive or with bool operads a
    >mistake? IMHO this is perfectly okay and I can't see why one should refrain
    >(however I might be overlooking something).


    But then, why do we need && and ||, we could simply use & and |
     
    Mats Weber, Jun 18, 2004
    #15
  16. Siemel Naran

    David Harmon Guest

    On Fri, 18 Jun 2004 02:51:51 +0200 in comp.lang.c++, Mats Weber
    <> wrote,
    >In article <cat1d8$8ca$>,
    > "Chris Theis" <> wrote:
    >
    >>Why do you consider using ^ bitwise exclusive or with bool operads a
    >>mistake? IMHO this is perfectly okay and I can't see why one should refrain
    >>(however I might be overlooking something).

    >
    >But then, why do we need && and ||, we could simply use & and |


    && does not evaluate the RHS if the LHS is false.
    || does not evaluate the RHS if the LHS is true.
    ^ has no similar equivalent.
     
    David Harmon, Jun 18, 2004
    #16
  17. Siemel Naran

    Siemel Naran Guest

    "David Harmon" <> wrote in message

    > It is probably a mistake to use ^ bitwise exclusive or with bool
    > operands. Use != instead.


    I have an objects a1 and a2 of type A. There is a functionA::something()
    that returns a bool. If one of a1 and a2 have something() == true, do I do
    some extra processing. Thus:

    if (a1->something() ^ a2->something()) {
    process(a1, a2);
    }

    I guess one could say

    if ((a1->something() && !a2->something()) || (!a1->something() &&
    a2->something())) {
    process(a1, a2);
    }

    But the intent of the first version looks clearer. Or there may be other
    ways that I've overlooked :).
     
    Siemel Naran, Jun 18, 2004
    #17
  18. "Siemel Naran" <> wrote in message
    news:duvAc.88391$...
    > "David Harmon" <> wrote in message
    >
    > > It is probably a mistake to use ^ bitwise exclusive or with bool
    > > operands. Use != instead.

    >
    > I have an objects a1 and a2 of type A. There is a functionA::something()
    > that returns a bool. If one of a1 and a2 have something() == true, do I

    do
    > some extra processing. Thus:
    >
    > if (a1->something() ^ a2->something()) {
    > process(a1, a2);
    > }
    >
    > I guess one could say
    >
    > if ((a1->something() && !a2->something()) || (!a1->something() &&
    > a2->something())) {
    > process(a1, a2);
    > }
    >
    > But the intent of the first version looks clearer. Or there may be other
    > ways that I've overlooked :).
    >


    As David said

    if (a1->something() != a2->something()) {
    process(a1, a2);
    }

    john
     
    John Harrison, Jun 18, 2004
    #18
  19. Siemel Naran

    Siemel Naran Guest

    "John Harrison" <> wrote in message
    news:2jff5nF10mb5fU1@uni-

    > As David said
    >
    > if (a1->something() != a2->something()) {
    > process(a1, a2);
    > }


    Duh, so obvious and I didn't see it :).
     
    Siemel Naran, Jun 18, 2004
    #19
  20. Siemel Naran

    Chris Theis Guest

    "David Harmon" <> schrieb im Newsbeitrag
    news:...
    > On Thu, 17 Jun 2004 23:11:04 +0200 in comp.lang.c++, "Chris Theis"

    <> wrote,
    > >Why do you consider using ^ bitwise exclusive or with bool operads a
    > >mistake? IMHO this is perfectly okay and I can't see why one should

    refrain
    > >(however I might be overlooking something).

    >
    > Convert from bool to int, then bitwise xor. How is that helpful?


    Well, I never claimed it to be helpful and absolutely share your opinion but
    I wouldn't consider it a "mistake" (however this might be nit-picking) ;-)

    >
    > Assuming you wanted a bool result, != is very clear as to what is intended

    and could have avoided this whole thread.

    Point taken and I absolutely agree!

    Cheers
    Chris
     
    Chris Theis, Jun 18, 2004
    #20
    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. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,047
    Grant Edwards
    Dec 16, 2005
  2. André
    Replies:
    3
    Views:
    1,618
  3. bdb112
    Replies:
    45
    Views:
    1,373
    jazbees
    Apr 29, 2009
  4. w i l l
    Replies:
    4
    Views:
    249
    Dan Brussee
    Jul 4, 2003
  5. Replies:
    10
    Views:
    317
    Thomas 'PointedEars' Lahn
    Feb 16, 2006
Loading...

Share This Page