Bitwise operator question...

Discussion in 'C++' started by littlehobo, Sep 30, 2003.

  1. littlehobo

    littlehobo Guest

    I'm trying to figure out this question i was asked but am having no luck!
    here's the question:

    "Write a Program that swaps the contents of two variables without
    using any other variable. Use one of the bitwise operators."

    I'm clueless... Any help would be appreciated!

    Thanks.
    littlehobo, Sep 30, 2003
    #1
    1. Advertising

  2. littlehobo

    Jack Klein Guest

    On Tue, 30 Sep 2003 02:57:36 GMT, "littlehobo" <>
    wrote in comp.lang.c++:

    > I'm trying to figure out this question i was asked but am having no luck!
    > here's the question:
    >
    > "Write a Program that swaps the contents of two variables without
    > using any other variable. Use one of the bitwise operators."
    >
    > I'm clueless... Any help would be appreciated!
    >
    > Thanks.


    Don't bother, it a crap question. There is no way to do this that is
    guaranteed not to invoke undefined behavior. People who ask questions
    like this don't know nearly as much about real C or C++ as they think.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Sep 30, 2003
    #2
    1. Advertising

  3. littlehobo

    Noah Roberts Guest

    Jack Klein wrote:
    > On Tue, 30 Sep 2003 02:57:36 GMT, "littlehobo" <>
    > wrote in comp.lang.c++:
    >
    >
    >>I'm trying to figure out this question i was asked but am having no luck!
    >>here's the question:
    >>
    >> "Write a Program that swaps the contents of two variables without
    >> using any other variable. Use one of the bitwise operators."
    >>
    >>I'm clueless... Any help would be appreciated!
    >>
    >>Thanks.


    CLUE: You want the ^ operator.
    >
    >
    > Don't bother, it a crap question. There is no way to do this that is
    > guaranteed not to invoke undefined behavior. People who ask questions
    > like this don't know nearly as much about real C or C++ as they think.


    What undefined behavior? This type of thing is used (or was at one
    time) a lot in graphics programming. I would be very interested in
    knowing what kind of unexpected behavior you could get from this technique.

    NR
    Noah Roberts, Sep 30, 2003
    #3
  4. littlehobo

    littlehobo Guest

    Thanks, for the replys! It took me a while...probably because I originally
    thought I was supposed to do it with one expression...I guess I was reading
    it "use one bitwise operator" or something. Anyway this is what I came up
    with:

    int a = 5, b = 8; //or whatever variables
    a ^= b;
    b ^= a;
    a ^= b;

    that took some time and paper lol!
    littlehobo, Sep 30, 2003
    #4
  5. [..]
    > int a = 5, b = 8; //or whatever variables
    > a ^= b;
    > b ^= a;
    > a ^= b;
    >
    > that took some time and paper lol!


    Well, I am not using bit operators, but the rule of associativity.

    a = (b+b)/2 + ((b=a)-a);


    --
    Vijay Kumar R Zanvar
    Vijay Kumar R Zanvar, Sep 30, 2003
    #5
  6. littlehobo

    lefoot Guest

    "littlehobo" <> wrote in message news:<3f78f0fa$0$12739$>...
    > I'm trying to figure out this question i was asked but am having no luck!
    > here's the question:
    >
    > "Write a Program that swaps the contents of two variables without
    > using any other variable. Use one of the bitwise operators."
    >
    > I'm clueless... Any help would be appreciated!
    >
    > Thanks.


    Use this fact " (a^b)^a = b, (a^b)^b = a "
    lefoot, Sep 30, 2003
    #6
  7. littlehobo

    Jerry Coffin Guest

    In article <blbt2g$ab9a8$-berlin.de>,
    says...

    [ ... ]

    > Well, I am not using bit operators, but the rule of associativity.
    >
    > a = (b+b)/2 + ((b=a)-a);


    ....and the nasal demons rear their ugly heads just like they do every
    time this subject arises.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 30, 2003
    #7
  8. littlehobo

    Jack Klein Guest

    On Mon, 29 Sep 2003 21:18:07 -0700, Noah Roberts
    <> wrote in comp.lang.c++:

    > Jack Klein wrote:
    > > On Tue, 30 Sep 2003 02:57:36 GMT, "littlehobo" <>
    > > wrote in comp.lang.c++:
    > >
    > >
    > >>I'm trying to figure out this question i was asked but am having no luck!
    > >>here's the question:
    > >>
    > >> "Write a Program that swaps the contents of two variables without
    > >> using any other variable. Use one of the bitwise operators."
    > >>
    > >>I'm clueless... Any help would be appreciated!
    > >>
    > >>Thanks.

    >
    > CLUE: You want the ^ operator.
    > >
    > >
    > > Don't bother, it a crap question. There is no way to do this that is
    > > guaranteed not to invoke undefined behavior. People who ask questions
    > > like this don't know nearly as much about real C or C++ as they think.

    >
    > What undefined behavior? This type of thing is used (or was at one
    > time) a lot in graphics programming. I would be very interested in
    > knowing what kind of unexpected behavior you could get from this technique.
    >
    > NR


    Case 1:

    double d1 = 3.14159;
    double d2 = 3.6864;

    double swap_contents_with_bitwise_operators(double d1, double d2)
    {
    double result = /* please fill in the blank */ ;
    return result;
    }

    Case 2:

    int swap_ints(int *a, int *b)
    {
    *a ^= *b;
    *b ^= *a;
    }

    Above can generate a trap representation, hence undefined behavior.
    But what about unsigned ints, you say?

    Consider the function above and...

    int x = 27;
    swap(&x, &x);

    ....not to mention that anyone who wrote this in production would be
    shot on sight. Well, OK, at the code inspection.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Oct 1, 2003
    #8
  9. littlehobo

    Jack Klein Guest

    On Mon, 29 Sep 2003 21:18:07 -0700, Noah Roberts
    <> wrote in comp.lang.c++:

    > Jack Klein wrote:
    > > On Tue, 30 Sep 2003 02:57:36 GMT, "littlehobo" <>
    > > wrote in comp.lang.c++:
    > >
    > >
    > >>I'm trying to figure out this question i was asked but am having no luck!
    > >>here's the question:
    > >>
    > >> "Write a Program that swaps the contents of two variables without
    > >> using any other variable. Use one of the bitwise operators."
    > >>
    > >>I'm clueless... Any help would be appreciated!
    > >>
    > >>Thanks.

    >
    > CLUE: You want the ^ operator.
    > >
    > >
    > > Don't bother, it a crap question. There is no way to do this that is
    > > guaranteed not to invoke undefined behavior. People who ask questions
    > > like this don't know nearly as much about real C or C++ as they think.

    >
    > What undefined behavior? This type of thing is used (or was at one
    > time) a lot in graphics programming. I would be very interested in
    > knowing what kind of unexpected behavior you could get from this technique.
    >
    > NR


    Case 1:

    double d1 = 3.14159;
    double d2 = 3.6864;

    double swap_contents_with_bitwise_operators(double d1, double d2)
    {
    double result = /* please fill in the blank */ ;
    return result;
    }

    Case 2:

    int swap_ints(int *a, int *b)
    {
    *a ^= *b;
    *b ^= *a;
    }

    Above can generate a trap representation, hence undefined behavior.
    But what about unsigned ints, you say?

    Consider the function above and...

    int x = 27;
    swap(&x, &x);

    ....not to mention that anyone who wrote this in production would be
    shot on sight. Well, OK, at the code inspection.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Oct 1, 2003
    #9
  10. littlehobo

    Noah Roberts Guest

    Jack Klein wrote:
    > On Mon, 29 Sep 2003 21:18:07 -0700, Noah Roberts
    > <> wrote in comp.lang.c++:


    >>What undefined behavior? This type of thing is used (or was at one
    >>time) a lot in graphics programming. I would be very interested in
    >>knowing what kind of unexpected behavior you could get from this technique.
    >>
    >>NR

    >
    >
    > Case 1:
    >
    > double d1 = 3.14159;
    > double d2 = 3.6864;
    >
    > double swap_contents_with_bitwise_operators(double d1, double d2)
    > {
    > double result = /* please fill in the blank */ ;
    > return result;
    > }


    I was pretty sure this was the case but I tested it anyway:

    test.cpp:11: error: invalid operands of types `double' and `double' to
    binary `
    operator^'
    test.cpp:11: error: in evaluation of `operator^=(double, double)'
    and more of the same...

    Correct me if my compiler is non-standard. I think C would barf also.

    Yes, there is probably a good reason why bitwise operators are not
    allowed on floating point types.

    >
    > Case 2:
    >
    > int swap_ints(int *a, int *b)
    > {
    > *a ^= *b;
    > *b ^= *a;
    > }


    Yes, you never ever want to do that.....

    > int x = 27;
    > swap(&x, &x);


    because the pointers might point to the same thing. This also would be
    undefined:

    x ^= x;

    or
    x += x; // yet you won't be saying += is a bad operation...

    I believe even 'x = x' is undefined.

    However, this sort of code is completely safe and quite useful:

    int x = 27, y = 34;
    x ^= y; y ^= x; x ^= y; // We know what x and y are after this...

    For instance, the easiest fastest way to highlight text in a GUI is to
    to use xor operators. You can also quickly switch colors with an xor
    operation as used in computer games (maybe not anymore):

    some_integral_type color_key = what_i_want ^ what_is_there;

    xor_draw_area(area, color_key); // switch
    xor_draw_area(area, color_key); // switch back.

    Also, you can make your function safe:

    void swap(int *x, int *y)
    {
    if (x==y) return; // no need to swap and it would be undefined if we
    did...

    ... // but it is safe otherwise...(assuming we own the memory
    pointed to by x and y which you also want to check).
    }

    >
    > ...not to mention that anyone who wrote this in production would be
    > shot on sight. Well, OK, at the code inspection.
    >


    Nobody really uses it to swap value x and y, but often you want to
    quickly change and revert values and xor is a great way to do that. It
    is used a lot in XFree86 and probably just about any GUI system around.

    NR
    Noah Roberts, Oct 1, 2003
    #10
  11. littlehobo

    Ron Natalie Guest

    "Jack Klein" <> wrote in message news:...

    >
    > int x = 27;
    > swap(&x, &x);
    >
    > ...not to mention that anyone who wrote this in production would be
    > shot on sight. Well, OK, at the code inspection.


    And for practical matters if you look at the code generated by most compilers for
    template <class T> inline void swap(T& a, T& b) {
    T t = a;
    a = b;
    b = t;
    }
    You'll find it involves fewer operations than the xor-hack. If T fits in a register
    it may not even involve an explicit temporary just the registers needed for
    the assignments:

    R1 <- a
    R2 <- b
    b <- R1
    a <- R2
    Ron Natalie, Oct 1, 2003
    #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. Peter Shaggy Haywood

    Re: bitwise operator on a struct

    Peter Shaggy Haywood, Jun 25, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,567
    Peter Shaggy Haywood
    Jun 25, 2003
  2. Peter Shaggy Haywood

    Re: bitwise operator on a struct

    Peter Shaggy Haywood, Jun 25, 2003, in forum: C Programming
    Replies:
    0
    Views:
    805
    Peter Shaggy Haywood
    Jun 25, 2003
  3. Mantorok Redgormor

    bitwise not operator

    Mantorok Redgormor, Oct 1, 2003, in forum: C Programming
    Replies:
    1
    Views:
    524
    Dan Pop
    Oct 1, 2003
  4. Manish_Ganvir
    Replies:
    13
    Views:
    1,555
    Keith Thompson
    Feb 14, 2005
  5. Ioannis Vranos
    Replies:
    8
    Views:
    326
    James Kanze
    Nov 14, 2008
Loading...

Share This Page