Skip to content

Commit 915121a

Browse files
add solution to problem number 5.2,
idea changes
1 parent 35887bf commit 915121a

File tree

2 files changed

+69
-24
lines changed

2 files changed

+69
-24
lines changed

.idea/workspace.xml

+54-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ch5PrimitiveTypes/5.2SwapBits.kt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ch5PrimitiveTypes
2+
3+
fun main(args: Array<String>) {
4+
println(swap(10, 0, 3))
5+
}
6+
7+
fun swap(x: Int, j: Int, i: Int): Int {
8+
var xCopy = x
9+
if(x.shr(i).and(1) != x.shr(j).and(1)){
10+
xCopy = x.xor(1.shl(i).or(1.shl(j)))
11+
}
12+
13+
return xCopy
14+
}
15+

0 commit comments

Comments
 (0)