```fun Int.floorEven() = this and 0x01.inv()``` I ...
# mathematics
m
Copy code
fun Int.floorEven() = this and 0x01.inv()
I was reading a project and i saw this code but i could not understand why this code flooring the number to even i know 01 inverse equals to -2 because of 2's complement equals to 1's complement - 1 and 2 complent is negative of that number but i could not understand why this code will always work?
i
This method actually nullizes the last bit of the number: if x = xn ...x2 x1 (n bits), then we actually do binary multiplication of it by 1...1 0 and get xn ... x2 0. This new number is always either equal to x (if x is even) or smaller than x by 1 (if x was odd). It works for both positive and negative numbers, you can check it.