Question 1: exclusive or exchange of two numbers
Suppose X = 2; y = 3. Our goal is (no tooth decay !!) Exchange the values of X and Y;
Take advantage of exclusive or particularity: x ^ x = 0
That is, the two identical numbers are 0;
Solution:
Make x = x ^ y = 2 ^ 3
Make y = x ^ y = 2 ^ 3 ^ 3 = 2 (x = x ^ y)
Make x = x ^ y ^ 2 = 2 ^ 3 ^ 2 = 3 (x = x ^ y; y = 2)
Question 2: Implement POW (int x, int y), that is, Power Y of X
The yth power of X is that there are Y x consecutive flights. The Code is as follows:
#include <stdio.h>#include <stdlib.h>int my_pow(int x,int y){ if(x==0) return 0; int ret=x,i=1; for(;i<y;i++){ ret=ret*x; printf("y=%d;ret=%d\n",i+1,ret); } return ret;}int main(){ int tmp = my_pow(2,10); printf("====%d\n",tmp); return 0;}
Result
[[email protected] Desktop]# ./a.outy=2;ret=4y=3;ret=8y=4;ret=16y=5;ret=32y=6;ret=64y=7;ret=128y=8;ret=256y=9;ret=512y=10;ret=1024====1024[[email protected] Desktop]#