位移运算符 交换变量值_如何在不使用临时变量或算术运算符的情况下交换两个数字?...

位移运算符 交换变量值

You have done swapping
of two numbers using temporary variable or by using arithmetic operators. It
can be done in following way.
Lets take two numbers
a=5 and b=7.
Using Temporary Variable
temp=a;            //temp becomes 5
a=b;                 //a becomes 7
b=temp;            //b becomes 5
Using Arithmetic Operators
a=a+b;              //a becomes 12
b=a-b;              //b becomes 5
a=a-b;               //a becomes 7
or
a=a*b;              //a becomes 35
b=a/b;              //b becomes 5
a=a/b;               //a becomes 7
In this article I am
sharing another method, may be you are already familiar with it. Swapping of
two numbers can also be done by using bitwise XOR operator i.e. ^. The XOR of two numbers a and b returns a number which has all the bits as 1 wherever bits of a and b differ. If bits are same then resultant bit will be 0.
For example binary of 5
is 0101 and 7 is 0111. If you do XOR of 5 and 7 then result will be 0010. A c program
is given below that uses this concept to swap values of two numbers.
#include <stdio.h>
int main()
{
  int
a=5,b=7;
 
a=a^b;  // a becomes 2 (0010)
 
b=a^b;  // b becomes 5 (0101)
 
a=a^b;  // a becomes 7 (0111)
 
printf(“After Swapping: a=%d,b=%d”,a,b);
  return 0;
}
If you have any doubts
or know any other method to swap values of two numbers then mention it in
comment section.
Happy Coding!! 🙂 🙂
You have done swapping
of two numbers using temporary variable or by using arithmetic operators . It
can be done in following way.
Lets take two numbers
a=5 and b=7.
Using Temporary Variable
temp=a;            //temp becomes 5
a=b;                 //a becomes 7
b=temp;            //b becomes 5
Using Arithmetic Operators
a=a+b;              //a becomes 12
b=a-b;              //b becomes 5
a=a-b;               //a becomes 7
or
a=a*b;              //a becomes 35
b=a/b;              //b becomes 5
a=a/b;               //a becomes 7
In this article I am
sharing another method, may be you are already familiar with it. Swapping of
two numbers can also be done by using bitwise XOR operator i.e. ^ . The XOR of two numbers a and b returns a number which has all the bits as 1 wherever bits of a and b differ. If bits are same then resultant bit will be 0.
For example binary of 5
is 0101 and 7 is 0111. If you do XOR of 5 and 7 then result will be 0010. A c program
is given below that uses this concept to swap values of two numbers.
#include <stdio.h>
int main()
{
  int
a=5,b=7;
 
a=a^b;  // a becomes 2 (0010)
 
b=a^b;  // b becomes 5 (0101)
 
a=a^b;  // a becomes 7 (0111)
 
printf(“After Swapping: a=%d,b=%d”,a,b);
  return 0;
}
If you have any doubts
or know any other method to swap values of two numbers then mention it in
comment section.
Happy Coding!! 🙂 🙂

翻译自: https://www.thecrazyprogrammer.com/2014/02/how-to-swap-two-numbers-without-using-temporary-variable-or-arithmetic-operators.html

位移运算符 交换变量值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值