is there any function like this? ```fun Int.coerce...
# stdlib
s
is there any function like this?
Copy code
fun Int.coerceIn(minMax: Int): Int {
    val abs = minMax.absoluteValue
    return coerceIn(-abs, abs)
}
z
Not afaik but that can be a one-liner:
Copy code
minMax.absoluteValue.let { int.coerceIn(-it, it) }
m
I hope there is no. It would be a terrible name for a stdlib function.
s
I agree but my question is what is the simplest way to get the positive and negative value of a number. Prefixing it with a
-
just reverses the sign and prefixing a number with a
+
does nothing.
f
The simplest way to switch the sign is with bit shifting. The most left bit is the sign. If it's 1 it's negative and if it's 0 it's positive. Now you can just shift it around to get what you need extremely fast.
s
Can you show me an example? I can't find documentation about it
f
s
I found nothing about bit shifting on that link and those answers dont work for me since I need both values (positive and negative) of a number that can be either positive or negative, but thank you anyway. Looks like the @Zach Klippenstein (he/him) [MOD] answer is the simplest way