From 107097c439378803dafbea63e90b0029b53b5e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=97=E9=9C=B2?= <69190413+illusory0x0@users.noreply.github.com> Date: Tue, 1 Jul 2025 19:05:57 +0800 Subject: [PATCH] feat(bigint): add `BigInt::to_int16` and `BitInt::to_uint16` --- bigint/bigint.mbt | 48 ++++++++++++++++++++++++++++++++++++++++++++++ bigint/bigint.mbti | 2 ++ 2 files changed, 50 insertions(+) diff --git a/bigint/bigint.mbt b/bigint/bigint.mbt index 63d828ce9..e29ae82b6 100644 --- a/bigint/bigint.mbt +++ b/bigint/bigint.mbt @@ -234,3 +234,51 @@ fn BigInt::signum(self : Self) -> Int { 1 } } + +///| +/// Converts a `BigInt` value to an unsigned 16-bit integer (`UInt16`). +/// +/// Parameters: +/// +/// * `self` : The `BigInt` value to be converted. +/// +/// Returns a `UInt16` value representing the lower 16 bits of the input +/// `BigInt`. +/// +/// Example: +/// +/// ```moonbit +/// let n = 42N +/// inspect(n.to_uint16(), content="42") +/// let neg = -1N +/// inspect(neg.to_uint16(), content="65535") // 2^16 - 1 +/// ``` +/// +pub fn BigInt::to_uint16(self : BigInt) -> UInt16 { + self.to_int().to_uint16() +} + +///| +/// Converts a `BigInt` value to a signed 16-bit integer (`Int16`). +/// +/// Parameters: +/// +/// * `self` : The `BigInt` value to be converted. +/// +/// Returns a 16-bit signed integer representing the lower 16 bits of the input +/// `BigInt`. +/// +/// Example: +/// +/// ```moonbit +/// let n = 42N +/// inspect(n.to_int16(), content="42") +/// let neg = -1N +/// inspect(neg.to_int16(), content="-1") +/// let big = 32768N // 2^15 +/// inspect(big.to_int16(), content="-32768") // Overflow to Int16.min_value +/// ``` +/// +pub fn BigInt::to_int16(self : BigInt) -> Int16 { + self.to_int().to_int16() +} diff --git a/bigint/bigint.mbti b/bigint/bigint.mbti index 50f33d037..ffd6573bf 100644 --- a/bigint/bigint.mbti +++ b/bigint/bigint.mbti @@ -33,10 +33,12 @@ fn BigInt::shl(Self, Int) -> Self fn BigInt::shr(Self, Int) -> Self fn BigInt::to_hex(Self, uppercase~ : Bool = ..) -> String fn BigInt::to_int(Self) -> Int +fn BigInt::to_int16(Self) -> Int16 fn BigInt::to_int64(Self) -> Int64 fn BigInt::to_octets(Self, length? : Int) -> Bytes fn BigInt::to_string(Self) -> String fn BigInt::to_uint(Self) -> UInt +fn BigInt::to_uint16(Self) -> UInt16 fn BigInt::to_uint64(Self) -> UInt64 impl Add for BigInt impl BitAnd for BigInt