What would be the most "correct" way to add two `U...
# getting-started
r
What would be the most "correct" way to add two `UByte`s together without overflow (e.g.
0xA3u + 0x91u
becomes
0xFFu
)? And the same for subtraction (e.g.
0x34u - 0x51u
becomes
0x00u
).
d
(a.toUInt() + b.toUInt()).coerceIn(0x00u, 0xFFu).toUByte()
r
And for subtraction would you convert to
Int
to handle negatives?
d
Ah, missed that part... yeah I guess I'd go to
Int
then, yeah
r
Cool, thanks. That's what I'm doing, I was just wondering if there's a better way I missed.
d
Not that I can think of anyways
👍 1