Bitwise operator question...

L

littlehobo

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.
 
J

Jack Klein

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
 
N

Noah Roberts

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
 
L

littlehobo

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!
 
V

Vijay Kumar R Zanvar

[..]
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);
 
L

lefoot

littlehobo said:
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 "
 
J

Jerry Coffin

[ ... ]
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.
 
J

Jack Klein

CLUE: You want the ^ operator.

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
 
J

Jack Klein

CLUE: You want the ^ operator.

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
 
N

Noah Roberts

Jack said:
On Mon, 29 Sep 2003 21:18:07 -0700, Noah Roberts



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
 
R

Ron Natalie

Jack Klein said:
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
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top