Analysis of "bit operator" Knowledge:
4. Bit operators
Tags: >> << & | ^
&: Bitwise AND, determine parity; do bitwise AND between any number and 1, if the result is 1, it is an odd number; if the result is 0, it is an even number
|: Bitwise OR, for Decimal rounding; any decimal is bitwise ORed with 0, and if it is saved, the integer part is taken
^: Bitwise XOR, used to exchange two numbers
a = a^b; b = b^a; a = a^b;
Abbreviation: a^=b; b^=a; a^=b;
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" style="text/css" href=""> <style></style> <script></script> </head> <body> <! 定义一个可以接收三个数字的函数,函数体内实现三个数字的排序输出 --> <script> function sortNum(a,b,c){ //如果将最小的数字赋值给a,将最大的数字赋值给c //1.比较a与b的大小关系,如果a>b的话,则交换两个数字,则a中存放的就是 a和b 的最小值。 a>b && (a^=b,b^=a,a^=b); //2.比较b与c的大小关系,如果b>c的话,则交换两个数字,则b中存放的就是 b和c 的最小值;则c中存放的是3个数字中的最大值。 b>c && (b^=c,c^=b,b^=c); //3.最后在比较a与b中的(两个最小值的大小)大小关系,如果a>b的话,则交换两个数字,则a中存放的就是 a和b 的最小值。 a>b && (a^=b,b^=a,a^=b); console.log(a,b,c); } </script> <button onclick="sortNum(52,36,98)">三个数字的排序</button> </body> </html>
The above is the detailed content of How to define a function that can receive three numbers and output. For more information, please follow other related articles on the PHP Chinese website!