filmov
tv
Swap two integers without using temp/third variable - Java Interview Questions -8

Показать описание
Swap two integers without using temp/third variable:
Method 1 (Using Arithmetic Operators):
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
Multiplication and division can also be used for swapping:
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
Method 2 (Using Bitwise XOR):
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
===================================================
Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
Follow me on my Facebook Page:
Let's join our Automation community for some amazing knowledge sharing and group discussion:
Follow me on my Facebook Page:
Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
Paid courses (Recorded) videos:
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
-------------------------------
✔️SOCIAL NETWORKS
--------------------------------
Support My Channel✔️Or Buy Me A Coffee
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
Method 1 (Using Arithmetic Operators):
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
Multiplication and division can also be used for swapping:
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
Method 2 (Using Bitwise XOR):
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
===================================================
Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
Follow me on my Facebook Page:
Let's join our Automation community for some amazing knowledge sharing and group discussion:
Follow me on my Facebook Page:
Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
Paid courses (Recorded) videos:
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
-------------------------------
✔️SOCIAL NETWORKS
--------------------------------
Support My Channel✔️Or Buy Me A Coffee
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
Комментарии