Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2006, 11:20 AM
Authorized User
 
Join Date: Oct 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Swap two integers without temp variable

Hi,

I have got a question of swapping two int variables without using any predefined builtin function and without using any temp variable.

if int a=3; int b=2, without using temp variable i have to get the output as a=2 and b=3.

How can i do it?


Ramkumar

A.D.Ramkumar
__________________
Ramkumar A D
 
Old February 26th, 2006, 07:32 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This should work.

a^=b^=a^=b;

More info on the ^= operator: http://msdn.microsoft.com/library/de...pOperators.asp

- Adam Kahtava [http://adam.kahtava.com]
 
Old February 28th, 2006, 08:03 PM
Registered User
 
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default

a += b;
b = a - b;
a = a - b;


Mark Chen
The Following User Says Thank You to MarkMHChen For This Useful Post:
Jitendra Malan (February 22nd, 2012)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Temp Table Variable in Stored Procedure rweide SQL Server ASP 2 April 15th, 2011 03:50 PM
Variable or parameter twice within the same temp Navy1991_1 XSLT 4 June 9th, 2008 02:29 PM
Swap Images Mike707 Dreamweaver (all versions) 3 December 18th, 2006 01:05 PM
swap image onclick The Beginner Javascript How-To 2 November 8th, 2006 08:42 AM
Problem to Swap two number in C# furqanms C# 1 June 5th, 2006 05:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.