# Idea: Deprecate casting method from signed integer to larger unsigned integer like Short.toUInt()

**URL:** <https://discuss.kotlinlang.org/t/idea-deprecate-casting-method-from-signed-integer-to-larger-unsigned-integer-like-short-touint/22855>\
**Category:** Libraries\
**Created:** [September 1, 2021, 3:48am UTC](https://discuss.kotlinlang.org/t/idea-deprecate-casting-method-from-signed-integer-to-larger-unsigned-integer-like-short-touint/22855 "2021-09-01T03:48:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anatawa12](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/anatawa12/32/6149_2.png) [@anatawa12](https://discuss.kotlinlang.org/u/anatawa12)\
**Post date:** [September 1, 2021, 3:48am UTC](https://discuss.kotlinlang.org/t/idea-deprecate-casting-method-from-signed-integer-to-larger-unsigned-integer-like-short-touint/22855/1 "2021-09-01T03:48:19Z")

</div>

I think the behavior of casting method from signed integer to larger unsigned integer like `Short.toUInt()` is not clear from function name because we can think two behaviors below:

1. cast to larger signed integer the first then cast to unsigned.
2. cast to unsigned integer the first then cast to larger unsigned.

Those behaviors are not same if the value is a negative integer.  
In the first behavior, upper bits are filled with 1 but 0 in the second behavior.  
In my opinion, this can make many bugs so it should not be used and everyone should explicitly cast in two steps like `Short.toInt().toUInt()` or `Short.toUShort().toUInt()` .  
So I think those methods should be deprecated.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [September 4, 2021, 10:27pm UTC](https://discuss.kotlinlang.org/t/idea-deprecate-casting-method-from-signed-integer-to-larger-unsigned-integer-like-short-touint/22855/2 "2021-09-04T22:27:44Z")

</div>

The sign extending behavior of `Short.toUInt()` was chosen because it is common in other languages with unsigned types, such as C, Rust, and Go, see this comment: [Unsigned integer types · Issue #135 · Kotlin/KEEP · GitHub](https://github.com/Kotlin/KEEP/issues/135#issuecomment-450003620)

---

<div class="post-metadata">

**Author:** ![stachenov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stachenov/32/6199_2.png) [@stachenov](https://discuss.kotlinlang.org/u/stachenov)\
**Post date:** [September 8, 2021, 5:03pm UTC](https://discuss.kotlinlang.org/t/idea-deprecate-casting-method-from-signed-integer-to-larger-unsigned-integer-like-short-touint/22855/3 "2021-09-08T17:03:21Z")

</div>

It almost makes sense… except that Kotlin is strongly connected to Java, and Java has the [`Short.toUnsignedInt()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html#toUnsignedInt-short-) method that does the exact opposite.

It’s a good thing Kotlin has `UShort` now, but it doesn’t always help, especially with Java interop. I still find myself often using `&/and 0xFFFF`.

---

<div class="post-metadata">

**Author:** ![anatawa12](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/anatawa12/32/6149_2.png) [@anatawa12](https://discuss.kotlinlang.org/u/anatawa12)\
**Post date:** [October 17, 2021, 8:14am UTC](https://discuss.kotlinlang.org/t/idea-deprecate-casting-method-from-signed-integer-to-larger-unsigned-integer-like-short-touint/22855/4 "2021-10-17T08:14:37Z")

</div>

I know many languages use such behavior but I think it’s better to depreciate because it’s confusing.  
If Kotlin thinks deprecation is too hard, It also is good to add an optional inspection in Kotlin, In my opinion.
