devxlogo

A Better Way to Swap Two Integer Variables

A Better Way to Swap Two Integer Variables

The algorithm shown in the tip, “Swap Values Using Only Two Variables” [“101 Tech Tips for VB Developers, Supplement to the February 1997 issue of VBPJ, page 25], crashes not only if (a+b) >= 32K, but if (a+b)

 a = a Xor b:	b = a Xor b:	a = a Xor b. 

This works for almost any programming language.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist