Skip to content

Commit 02c0f81

Browse files
committed
Merge branch 'main' into renovate/sandwich
2 parents 36b25ff + b3c8c46 commit 02c0f81

File tree

5 files changed

+133
-10
lines changed

5 files changed

+133
-10
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@
2222

2323
### Metrics
2424
```text
25-
14993 number of properties
26-
10384 number of functions
27-
8855 number of classes
28-
229 number of packages
29-
3486 number of kt files
25+
15001 number of properties
26+
10391 number of functions
27+
8857 number of classes
28+
230 number of packages
29+
3488 number of kt files
3030
```
3131

3232

3333
### Complexity Report
3434
```text
35-
259493 lines of code (loc)
36-
158946 source lines of code (sloc)
37-
115976 logical lines of code (lloc)
38-
72487 comment lines of code (cloc)
39-
24701 cyclomatic complexity (mcc)
35+
259554 lines of code (loc)
36+
158990 source lines of code (sloc)
37+
116006 logical lines of code (lloc)
38+
72493 comment lines of code (cloc)
39+
24708 cyclomatic complexity (mcc)
4040
20232 cognitive complexity
4141
0 number of total code smells
4242
45 comment source ratio

api/Kotlin-Lab.api

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21732,6 +21732,13 @@ public final class dev/shtanko/kotlinlang/types/phantom/Open : dev/shtanko/kotli
2173221732
public static final field INSTANCE Ldev/shtanko/kotlinlang/types/phantom/Open;
2173321733
}
2173421734

21735+
public final class dev/shtanko/kotlinlang/types/unsigned/UnsignedKt {
21736+
public static final field BIG_COUNTER J
21737+
public static final fun buffers ()V
21738+
public static final fun unsignedBitwiseAnd-7apg3OU (B)B
21739+
public static synthetic fun unsignedBitwiseAnd-7apg3OU$default (BILjava/lang/Object;)B
21740+
}
21741+
2173521742
public final class dev/shtanko/kotlinlang/value/Dp : java/lang/Comparable {
2173621743
public static final field Companion Ldev/shtanko/kotlinlang/value/Dp$Companion;
2173721744
public static final field MULTIPLY_FACTOR F
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
✅ 1. Binary & Bitwise Operations
2+
Unsigned types are helpful when doing bit manipulation, as they prevent issues with sign extension and simplify logic.
3+
4+
```kotlin
5+
val mask: UByte = 0b11110000u
6+
val value: UByte = 0b10101010u
7+
val result = value and mask // no unexpected sign problems
8+
```
9+
10+
✅ 2. Working with Raw Data / Buffers
11+
When you're processing binary files, network packets, or image data, values are often naturally unsigned.
12+
13+
```kotlin
14+
val bytes: UByteArray = ubyteArrayOf(0xFFu, 0x01u, 0x7Fu)
15+
```
16+
17+
✅ 3. Interfacing with C / Native Code (JNI / NDK / WASM)
18+
If you're using Kotlin/Native or Kotlin/Multiplatform and talking to native libraries, they often expect unsigned types.
19+
20+
```kotlin
21+
external fun nativeRead(buffer: UByteArray): UInt
22+
```
23+
24+
✅ 4. Avoiding Negative Values by Design
25+
If a value should never be negative (like an ID, count, size, etc.), unsigned types make your intention clearer and catch errors earlier.
26+
27+
```kotlin
28+
fun createUser(id: UInt) { /* ... */ }
29+
```
30+
31+
✅ 5. Cryptography / Checksums / Hashes
32+
Crypto algorithms and checksums often rely on unsigned integer arithmetic.
33+
34+
```kotlin
35+
val temperatureRaw: UShort = readSensor()
36+
```
37+
38+
✅ 6. Embedded / IoT / Microcontroller Code
39+
For constrained environments or devices (like Arduino, STM32, etc.), values are usually unsigned and memory-efficient.
40+
41+
```kotlin
42+
val temperatureRaw: UShort = readSensor()
43+
```
44+
45+
✅ 7. Performance & Precision with Large Positive Numbers
46+
Sometimes you need the extra upper range — e.g. ULong gives you up to 2^64 - 1, while Long is capped at 2^63 - 1.
47+
48+
```kotlin
49+
val bigCounter: ULong = 18_446_744_073_709_551_615u
50+
```
51+
52+
A note of caution 🚧
53+
Kotlin’s unsigned types are not Java-compatible, meaning:
54+
55+
You can't pass UInt directly to Java code (you’ll need to convert it to Int)
56+
57+
Some standard library functions may not work the same way
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@file:OptIn(ExperimentalUnsignedTypes::class)
2+
3+
package dev.shtanko.kotlinlang.types.unsigned
4+
5+
fun unsignedBitwiseAnd(value: UByte = 0b10101010u): UByte {
6+
val mask: UByte = 0b11110000u
7+
return value and mask // no unexpected sign problems
8+
}
9+
10+
fun buffers() {
11+
val bytes: UByteArray = ubyteArrayOf(0xFFu, 0x01u, 0x7Fu)
12+
}
13+
14+
/**
15+
* Sometimes you need the extra upper range — e.g. ULong gives you up to 2^64 - 1,
16+
* while Long is capped at 2^63 - 1.
17+
*/
18+
const val BIG_COUNTER: ULong = 18_446_744_073_709_551_615u // ULong.MAX_VALUE
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dev.shtanko.kotlinlang.types.unsigned
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Nested
5+
import org.junit.jupiter.api.Test
6+
7+
class UnsignedTest {
8+
9+
@Nested
10+
inner class UnsignedBitwiseAndTest {
11+
@Test
12+
fun `unsignedBitwiseAnd with default value`() {
13+
val result = unsignedBitwiseAnd()
14+
assertEquals(0b10100000u.toUByte(), result)
15+
}
16+
17+
@Test
18+
fun `unsignedBitwiseAnd with all ones`() {
19+
val result = unsignedBitwiseAnd(0b11111111u)
20+
assertEquals(0b11110000u.toUByte(), result)
21+
}
22+
23+
@Test
24+
fun `unsignedBitwiseAnd with all zeros`() {
25+
val result = unsignedBitwiseAnd(0b00000000u)
26+
assertEquals(0b00000000u.toUByte(), result)
27+
}
28+
29+
@Test
30+
fun `unsignedBitwiseAnd with mixed bits`() {
31+
val result = unsignedBitwiseAnd(0b01010101u)
32+
assertEquals(0b01010000u.toUByte(), result)
33+
}
34+
35+
@Test
36+
fun `unsignedBitwiseAnd with different mixed bits`() {
37+
val result = unsignedBitwiseAnd(0b10011001u)
38+
assertEquals(0b10010000u.toUByte(), result)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)