473,785 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace values

I'm using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any other
variables or functions.

Hrcko
Oct 3 '07 #1
19 1448
On Oct 3, 10:22 am, "Hrvoje Voda" <hrvoje.vo...@z g.htnet.hrwrote :
I'm using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any other
variables or functions.
There's a very hacky way of doing it, but using a temporary variable
would certainly be simpler and more readable.

Can I ask *why* you don't want to use any other variables or methods?

Jon

Oct 3 '07 #2
Because it's a test question that I have to solve for a job.

Hrcko
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
On Oct 3, 10:22 am, "Hrvoje Voda" <hrvoje.vo...@z g.htnet.hrwrote :
>I'm using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any
other
variables or functions.

There's a very hacky way of doing it, but using a temporary variable
would certainly be simpler and more readable.

Can I ask *why* you don't want to use any other variables or methods?

Jon

Oct 3 '07 #3
x = x ^ y;
y = y ^ x;
x = x ^ y;

Graeme
"Doing his bit to rid the world of idiotic aha! interview questions"

"Hrvoje Voda" <hr**********@z g.htnet.hrwrote in message
news:fd******** **@ss408.t-com.hr...
Because it's a test question that I have to solve for a job.

Hrcko
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
>On Oct 3, 10:22 am, "Hrvoje Voda" <hrvoje.vo...@z g.htnet.hrwrote :
>>I'm using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any
other
variables or functions.

There's a very hacky way of doing it, but using a temporary variable
would certainly be simpler and more readable.

Can I ask *why* you don't want to use any other variables or methods?

Jon

Oct 3 '07 #4
On Oct 3, 11:04 am, "Hrvoje Voda" <hrvoje.vo...@z g.htnet.hrwrote :
Because it's a test question that I have to solve for a job.
Does your potential employer know that you're asking for help online?
Are they comfortable with that?

Jon

Oct 3 '07 #5
On 3 , 12:22, "Hrvoje Voda" <hrvoje.vo...@z g.htnet.hrwrote :
I'm using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any other
variables or functions.

Hrcko
You may use System.Threadin g.Interlocked to do it:

int var1 = 5;
int var2 = 10;
var1 = System.Threadin g.Interlocked.E xchange(ref var2, var1);

But, maybe, you are tested not on knowing of hacks but on ability to
write understandable code? :)

Regards,
Mykola
http://marss.co.ua

Oct 3 '07 #6
"marss" <ma******@gmail .comwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
>
You may use System.Threadin g.Interlocked to do it:

int var1 = 5;
int var2 = 10;
var1 = System.Threadin g.Interlocked.E xchange(ref var2, var1);
LOL!
Excellent solution.
Playing around with bits are sooo oldschool :)

- Michael Starberg
Oct 3 '07 #7
Michael S wrote:
"marss" <ma******@gmail .comwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
>You may use System.Threadin g.Interlocked to do it:

int var1 = 5;
int var2 = 10;
var1 = System.Threadin g.Interlocked.E xchange(ref var2, var1);

LOL!
Excellent solution.
Playing around with bits are sooo oldschool :)

- Michael Starberg
You don't have to play around with bits. It works just fine with
addition and subtraction:

var1 -= var2
var2 += var1
var1 = var2 - var1
--
Göran Andersson
_____
http://www.guffa.com
Oct 3 '07 #8
On 3 , 19:27, Göran Andersson <gu...@guffa.co mwrote:
You don't have to play around with bits. It works just fine with
addition and subtraction:

var1 -= var2
var2 += var1
var1 = var2 - var1
Not so reliable as Graeme's solution. What about stack overflow? :)

Regards,
Mykola
http://marss.co.ua

Oct 3 '07 #9
marss <ma******@gmail .comwrote:
On 3 , 19:27, Göran Andersson <gu...@guffa.co mwrote:
You don't have to play around with bits. It works just fine with
addition and subtraction:

var1 -= var2
var2 += var1
var1 = var2 - var1
Not so reliable as Graeme's solution. What about stack overflow? :)
I can't see how the stack would overflow. You might get *numeric*
overflow, but that would just wrap round - shouldn't be a problem.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2255
by: andrea.gavana | last post by:
Hello NG, probably this is a basic question, but I'm going crazy... I am unable to find an answer. Suppose that I have a file (that I called "Errors.txt") which contains these lines: MULTIPLY 'PERMX' @PERMX1 1 34 1 20 1 6 / 'PERMX' @PERMX2 1 34 21 41 1 6 / 'PERMX' @PERMX3 1 34 1 20 7 14 /
3
1686
by: Roy Adams | last post by:
Hi group I'm having trouble using the replace command Here's my code below <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%> <!--#include file="../../Connections/conn.asp" --> <% if( String(Request.Form("ProductName")) != "undefined" ){//formfield
1
1888
by: Will | last post by:
I have the following code: var rx = /{{(.+?)}}/i; var expr = 'each {{word}} wrapped in {{curly}} {{braces}} in this {{string}} needs to be {{replaced}} with a different {{value}}.'; var values = ; values = "foo"; values = "bar"; values = "bundy";
3
2428
by: RickN | last post by:
What is the best way to open a file and perform a search and replace for multiple char values. Thanks, RickN
3
5452
by: Andy Sutorius | last post by:
Hi, I read the thread (2/16/05) regarding a replace function in C# however it didn't answer my question. I have a string which is building an insert sql statement and I would like to replace apostrophes of the form fields. I was trying to do something like this: string sqlInsertEmails = "insert into tblContent (content, subject) values ('" + Replace(txtBody.Text,"'","''") + "', '" + Replace(txtSubject.Text,"'","''") + "')";
12
7323
by: Michael | last post by:
In PHP there is a function called str_replace (http://php.net/str_replace). Basically you can freed in two strings and a "subject" string. Then it goes through the subject string searching for occurences of the "search" string and replaces them with the "replace" string. Is there something simular in JavaScript, or can someone give me a solution. I am an experienced PHP user and XHTML writer, and I have learnt Javascript to a reasonable...
2
1577
by: john | last post by:
In a table I have text field A. I would like to replace all the null values in field A to a real value, let's say 'Test'. When I use Find & Replace and I search for 'is null' and I press replace or replace all, no null value is found, so I can't replace anything. However, when I press Find Next in that same dialog then the null values are found. Anyone and idea what is happening here? Thanks, john
1
1503
by: nasse | last post by:
Need some one to help me in doing a find and replace task for all the files in my application. I have a list of pairs of old values in one file (findFile.php) and wanted to replace them with different values that are store in second file (replaceFile.php): findFile.php: , "abdomen", "Abdomen"; , "afternoon", "Afternoon"; , "almost always", "Almost Always"; , "almost never", "Almost Never"; , "always", "Always"; , "bursitis",...
2
1574
by: wolverine2512 | last post by:
Hi all! I want to replace the values of QID in my datalist1 bound with the following data: Dim sqlStmnt As String = "Select QID, QUESTION from ENVIRONMENTAL_EXAM order by newid() " Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmnt, scon) Dim ds As New DataSet
2
11684
by: X l e c t r i c | last post by:
Here: http://bigbangfodder.fileave.com/res/sandr.html I'm trying to use string.replace() for a basic search and replace form using textarea values as the regexp and replacement values for string.replace(). When I tried to use the textarea variable name for regexp it didn't work as I thought it would. For example:
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.