8000 feat(bigint): add `BigInt::to_int16` and `BitInt::to_uint16` by illusory0x0 · Pull Request #2387 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(bigint): add BigInt::to_int16 and BitInt::to_uint16 #2387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions bigint/bigint.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 2 additions & 0 deletions bigint/bigint.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
0