Returns an exclusive or exchanged number. Pow (int x, int Y) is implemented, that is, the power of Y of X.

Source: Internet
Author: User

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]# 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.