异或运行符^,相同为0,不同为1
function MyClick(){ /* 乘法表 var shtml = "<table>"; for(var i=1; i<=9; i++){ shtml += "<tr>"; for(var j=1; j<=i; j++ ){ shtml += "<td>" + i + "*" + j + "=" + i * j + "</td>"; } shtml +="</tr>"; } shtml += "</table>"; document.write(shtml);*/ var anumber = [5,10,8,9,12,32]; var inum = 0; for(var i=0; i<=anumber.length - 2; i++){ for(var j=i+1 ; j<=anumber.length - 1; j++){ if(anumber[i] > anumber[j]){ inum = anumber[i]; anumber[i] = anumber[j]; anumber[j] = inum; /* 也可以用异或运算符的写法 anumber[i] = anumber[i]^anumber[j]; anumber[j] = anumber[j]^anumber[i]; anumber[i] = anumber[i]^anumber[j]; */ } } } alert(anumber.toString()); }
原文:http://www.cnblogs.com/StanLong/p/6686949.html