How to Swap two numbers without using third variable? Bit Manipulation Interview - XOR

Опубликовано: 01 Октябрь 2024
на канале: Firmware
609
15

In this video, we look into an XOR property that helps us swap two numbers using bit manipulations without requiring a third variable in C/C++. This is a commonly asked interview question for software/firmware /embedded roles.
There are other methods available to do the same, such as a combination of multiplication - division, or addition-subtraction. But these methods have a disadvantage against using XOR. For using addition or multiplication, we would have to limit the range of input, else the resultants could go out of range.
Also, a helpful insight that we find through this problem is the associative property and commutative property of XOR operations.

Must know concepts of XOR:    • Must know concepts of XOR -  Bit Mani...  
Bit Manipulation basic techniques:    • Bit Manipulation - Three most used fu...  

See Bit Manipulation Interview Preparation Series here:
   • Bit Manipulation Interview Series  

#XOR #BitManipulation #BitAlgorithm

You may follow us on Facebook and Instagram:
Facebook :   / firmwarecode  
Instagram :   / firmwarecode