
Xor swap algoridam

sanganak programingmadhye, vishesh kinwa adlabadal (kadhikadhi sankshipt xor adlabadal) haah eka algoridam aahe joou samanyatah aavashyak asalelya tatpuratya chalancha vapar naan karata done chalanchi moolye adlabadal karnyasathi vishesh kinwa bitwise operation vaparato.
algoridam hii pramukhyane eka naveenata aahe aanee 'exclusive' kinwa 'operation' chey gunadharm darshavinyacha eka marg aahe. kadhikadhi yachee charcha karyakram optimization mhanoon kelee jaate, parantu ashee konatihi prakarane naheet jithe 'exclusive' kinwa 'exclusive' dware adlabadal kelyane manak, spasht tantrapeksha fayda hoto.
algoridam.
paramparik adlabadal karnyasathi tatpurte sanchayan chull vaparane aavashyak aahe. tathapi, zore swap algoridamcha vapar karoon, tatpuratya sathavanukichi aavashyakta nahee. algoridam khalilpramane aaheh
x: = x xor y;// xor moolye aanee x madhye parinam sanchayit qaraa
y: = y xor x;// xor moolye aanee y madhye parinam sanchayit qaraa
x: = x xor y;// xor moolye aanee x madhye parinam sanchayit qaraa
Xor heey eka parivartaneey operation asalyane, ektar x xor y kinwa y xor x vareel teen olinpaiki konatyahi ekachya badalyat vaparale jaau shakate. lakshat ghya qee kahee sanrachananvar, xor nirdeshacha pahila operand, jyaa thikani operationcha parinam sangrahit keela jaato tyaa lakshya sthanas nirdisht karato, jyamule hii adlabadlata rokhali jaate. algoridam samanyatah teen machine-code soochananshi sambandhit asato, jeey khaleel taktyachya teen olinmadhye sambandhit seudocode aanee assembly soochanandware darshavile jaateh 
vareel pranali/370 vidhansabha code samplemadhye, aar1 aanee aar2 heey vegale register aahet aanee pratyek operation tyache parinam pahilya yuktivadat navachya rajistarmadhye sodate. x86 assemblicha vapar karoon, x aanee y hii moolye anukrame ex aanee ebx (anukrame) nondinmadhye aahet aanee operationcha parinam pahilya nondinmadhye thevatat.
tathapi, seudocode kinwa uchch-stariy bhashechya aavruttimadhye kinwa amalbajavanimadhye, jarr "x" aanee "y" samaan sathavan sthan vaparlyas algoridam ayashasvi hoto, kaaran tyaa thikani sathavalele moolya pahilya xor nirdeshadvare shoonya kele jaeel aanee nantar shoonya raheel; teey "swatashi adlabadal kele jaanar nahee". jarr 'x' aanee 'y' chee moolye samaan asateel tarr heey 'nahee' sarkhech aahe. jevha "x" aanee "y" samaan sathavan sthan vapartat tevhach samasya udbhavate, asha paristhitit tyanchi moolye aadhich samaan asane aavashyak aahe. mhanje, jarr "x" aanee "y" samaan sathavan sthan vaparat asateel, tarr olah
x: = x xor y
x" laa shunyavar seet karate (kaaran "x" = "y" mhanoon x xor y shoonya aahe) "aanee" y "laa shunyavar seet karate (kaaran teey samaan sathavan sthan vaparte), jyamule" x "aanee" y "tyanchi mool moolye gamavatat.
achookatecha purava.
dwaiti karya xor lambichya sootra _ 1 chya biit stringswar khaleel gunadharm darshavite (jethe sootra _ 2 xor darshavite):
samaja, khaleel taktyapramane, 'aaa' aanee 'buub' yaa prarambhik moolyansah, aaplyakade anukrame 'cocus _ 1' aanee 'cocus _ 2' yaa done vegalya nondi aahet. aamhi khalee dilelya kramane karya karato aanee varr soochibaddha kelelya gunadharmancha vapar karoon aamche parinam qamee karato.
rekheey bijganitacha arth.
zorcha arth dvimiteey jode mhanoon lavala jaau shakato aanee done ghatkansah kshetravaril dvimiteey vector jaaget bitschya jodicha arth vector mhanoon lavala jaau shakato, algoridammadhil payanyancha arth done ghatkansah kshetravaril 2 × 2 matrixney gunakar mhanoon lavala jaau shakato. sadhepanasathi, suruvatila asey gruheet dhara qee "x" aanee "y" heey pratyek biit aahet, biit vector naheet.
udaharanarth, payarih
x: = x xor y
jyat antarbhut dekhil aaheh
y: = y
matricks sootra _ 9 shhee anuroop aahe
tyanantar kriyancha kram asha prakare vyakt keela jaatoh
(dwaiti moolyansah kaam karane, mhanoon sootra _ 12), jeey done pankti (kinwa stambh) badalanyache prathamik matricks dusya ghatakat jodanyachya sankramananchya (katarane) sandarbhat vyakt karate.
jethe x aanee y heey eka biit naheet, parantu tyaeivaji "n" lambiche biit vector aahet, tyaa thikani samanyikaran karnyasathi, yaa 2 × 2 matrixchi jaaga sootra _ 13 sarkhya 2 "n" × 2 "n" block matrixney ghetali jaate.
heey matricks 'variables' varr navhe tarr 'values' varr kaam karat aahet (sathavan sthanansah), mhanoon haah arth sathavan sthan aanee samaan sathavan sthan samayik karananya donhi chalanchya samasyanpasun door sarato.
code udaharan.
eka see karya jeey zore swap algoridam lagoo karateh
shoonya xorswap (int * x, int * y) 
 jarr (x!= vaay)
 *x = * y;
 *y = * x;
 *x = * y;
code pratham patte vegale aahet qaa teey tapasato. anyatha, jarr teey samaan asateel, tarr algoridam tiheri cockoge _ 3 madhye dumdel jyacha parinam shoonya hoil.
zore swap algoridamchi vyakhya macross dekhil kelee jaau shakateh
 (aaa) = (buub), (buub) = (aaa), \
 (aaa) a aanee b ekach vastuvar asatana/* karya karat nahee-shoonya niyukt karate \
 (0) tyaa babatit vastoola */
 ((& (aaa) = = & (buub))? (aaa)/* vegvegale patte tapasa * / \
 : xorswap _ unsecup (a, b))
vyavharat talhanyachi karane.
aadhunik see. pii. yuu. vastukalevar, zore tantra adlabadal karnyasathi tatpurte chull vaparnyapeksha haloo asoo shakate. kiman alikadil x86 see. pii. yuu. varr, aii. emm. dii. aanee intell yaa donhidware, nondinmadhye niyamitpane haltana shoonya vilamb hoto. (yala move-elemination mhanatat.) jaree vaparnyasathi konatihi sthapatyashastriy nond upalabdh nasali taree, teen zhore ektrit kelyapramane kiman kochee _ 4 soochana vegwan asel. aanakhi eka kaaran mhanje aadhunik cpu nirdesh pipelinedware samantar soochana amalat aananyacha prayatna karato. zore tantrat, pratyek operationsathiche input mageel operationchya nikalanvar avalambun asatat, mhanoon teey katekorpane anukramik kramane amalat aanane aavashyak aahe, soochana-stariy samantarateche konatehi fayade nakarane.
aliasing.
zore swap dekhil vyavharat aliasingdware klisht aahe. jarr ekhadya thikanchya samagrichi swatashi adlabadal karnyacha prayatna keela gela tarr tyacha parinam assa hoto qee sthan shoonya jhaale aahe aanee tyache moolya gamavale aahe. mhanoon, jarr aliasing shakya asel tarr uchch-stariy bhashet zore adlabadal aandhalepanane vaparale jaau naye. done nondinchya samagrichi adlabadal karnyasathi heey tantra asemblimadhye vaparale asalyas hii samasya lagoo hott nahee.
gensenchya upkaranapramanech, navanusar coll kartana ashach samasya udbhavtat, jithe tatpuratya chaladware cockoge _ 5 aanee cockoge _ 6chee adlabadal kelyane yuktivad sambandhit asalyamule chukiche parinam midtatah cockoge _ 7 dware adlabadal kelyane dusya vidhanat cockoge _ 5 chey moolya badalate, jyacha parinam tisya vidhanat cockoge _ 6 sathee chukiche cockoge _ 5 moolyat hoto.
vividhata.
Xor swap algoridamche antarnihit tattva vareel ell 1 teey ell 4 paryantchya konatyahi operation nikashanna lagoo kele jaau shakate. jode aanee vaja karoon zorchi jaaga ghetalyane thodi vegali, parantu mothya pramanat samatulya, rachana milate. udaharanarth,
shoonya adswap (swakshari naan kelele int * x, swakshari naan kelele int * y)
 jarr (x!= vaay)
 *x = * x + * y;
 *y = * x-* y;
 *x = * x-* y;
Xor swapchya ulat, yaa badalasathi aavashyak aahe qee antarnihit processor kinwa programming bhasha modular ankganit kinwa dwiganak yasarkhi paddhat vaparte jenekarun purnank overflomule cockoge _ 11 chya gananemule truti houu shakat nahee. tyamule, heey vyavharat zore swappeksha adhik kvachitach disun yete.
tathapi, see programming bhashet vareel cockoge _ 12 chee amalbajavani purnank overflochya babatit dekhil nehmich karya karate, kaaran see mankanusar, swakshari naan kelelya purnankanchi jode aanee vaja heey modular ankganitachya niyamanche palan karate, i. eae. heey chakriy gut sootra 14 madhye kele jaate jethe sootra 15 hii cockoge 13 chya bitschee sankhya aahe. kharokhar, algoridamchi achookata konatyahi abelian gatamadhye sootra sootra 16 aanee sootra 17 dharan karatat yaa vastusthitivarun yete. heey zore swap algoridamsathi purava samanya karateh zore heey abelian gut sootra _ 18 madheel jode aanee vaja donhi aahe (jeey sootra _ 19 chya "s" pratinchi thet berries aahe).
cockoge _ 14 prakarashi (cockoge _ 15 sathee purvanirdharit) vyavhar kartana heey lagoo hott nahee. swakshari kelele purnank atipravah heey see madheel eka anirdharit vartan aahe aanee asha prakare mankadware modular ankganitachi hummy dilee jaat nahee, jyamule chukiche parinam houu shakatat.
cocksey _ 12 madheel karyancha kram matricks gunakaradvare asha prakare vyakt keela jaau shakatoh
vatpachya nondanisathi arj.
samarpit adlabadal soochana nasalelya sanrachananvar, kaaran teey atirikt tatpurte nondani talhate, ishtatam nondani vatpasathi zore adlabadal algoridam aavashyak aahe. nondani vatpasathi sthir ekal assignment form vaparnare sankalkansathi heey visheshtah mahatvache aahe; heey sankalak adhunmadhun asey program tayar karatat jyanna konatehi nondani vinamulya nastana done nondani adlabadal karane aavashyak asate. zore swap algoridam atirikt nondani aarakshit karanyachi kinwa mukhya smrutimadhye konatehi nondke takanyachi garaj talhate. jode/vaja prakar dekhil tyach uddeshasathi vaparla jaau shakato.
nondani vatpachi hii paddhat jee. pii. yuu. shader sankalkansathi visheshtah sambandhit aahe. aadhunik jee. pii. yuu. sanrachananvar, maryadit memory bandwidth aanee uchch memory vilambatemule spilling variables mahag aahet, tarr rajistarcha vapar maryadit kelyane register filechya gatishil vibhajanamule kamgiri sudharu shakate. tyamule kahee jee. pii. yuu. compilerna zore swap algoridam aavashyak aahe.