Last active
August 19, 2019 09:33
-
-
Save sahajre/ec8ba50321fba2bc3cae841ce3751860 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// Swapping with Assignment | |
i := 1 | |
j := 2 | |
i, j = j, i | |
fmt.Println(i, j) // 2 1 | |
// with XOR | |
i = 1 | |
j = 2 | |
i = i ^ j | |
j = i ^ j | |
i = i ^ j | |
fmt.Println(i, j) // 2 1 | |
// with multiplication and division | |
i = 1 | |
j = 2 | |
i = i * j | |
j = i / j | |
i = i / j | |
fmt.Println(i, j) // 2 1 | |
// with addition and substraction | |
i = 1 | |
j = 2 | |
i = i + j | |
j = i - j | |
i = i - j | |
fmt.Println(i, j) // 2 1 | |
// with temporary variable | |
i = 1 | |
j = 2 | |
temp := i | |
i = j | |
j = temp | |
fmt.Println(i, j) // 2 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment