Exchange two variables without having to open space.

xiaoxiao2021-03-06  45

Interviewer asked: "How to exchange two variables without opening a space." (Preferably, the resources are valuable in the embedded system).

The answer is that the use bit is as follows:

Void

SWAP

int

a,

int

b)

{A = a ^ b; b = b ^ a; a = a ^ b;}

The examiner said that if A, b is full of 32 digits, the bit operation will overflow.

I don't know if it is correct.

Void

SWAP

int

a,

int

b)

{A = a b; b = a-b; a = a-b;}

转载请注明原文地址:https://www.9cbs.com/read-63179.html

New Post(0)