Rajeev Ranjan
Rajeev Ranjan

Reputation: 4096

Can a BigInteger be truncated to an i32 in Rust?

In Java, intValue() gives back a truncated portion of the BigInteger instance. I wrote a similar program in Rust but it appears not to truncate:

extern crate num;

use num::bigint::{BigInt, RandBigInt};
use num::ToPrimitive;

fn main() {
    println!("Hello, world!");
    truncate_num(
        BigInt::parse_bytes(b"423445324324324324234324", 10).unwrap(),
        BigInt::parse_bytes(b"22447", 10).unwrap(),
    );
}

fn truncate_num(num1: BigInt, num2: BigInt) -> i32 {
    println!("Truncation of {} is {:?}.", num1, num1.to_i32());
    println!("Truncation of {} is {:?}.", num2, num2.to_i32());
    return 0;
}

The output I get from this is

Hello, world!
Truncation of 423445324324324324234324 is None.
Truncation of 22447 is Some(22447).

How can I achieve this in Rust? Should I try a conversion to String and then truncate manually? This would be my last resort.

Upvotes: 4

Views: 3156

Answers (2)

kennytm
kennytm

Reputation: 523584

Java's intValue() returns the lowest 32 bits of the integer. This could be done by a bitwise-AND operation x & 0xffffffff. A BigInt in Rust doesn't support bitwise manipulation, but you could first convert it to a BigUint which supports such operations.

fn truncate_biguint_to_u32(a: &BigUint) -> u32 {
    use std::u32;
    let mask = BigUint::from(u32::MAX);
    (a & mask).to_u32().unwrap()
}

Converting BigInt to BigUint will be successful only when it is not negative. If the BigInt is negative (-x), we could find the lowest 32 bits of its absolute value (x), then negate the result.

fn truncate_bigint_to_u32(a: &BigInt) -> u32 {
    use num_traits::Signed;

    let was_negative = a.is_negative();
    let abs = a.abs().to_biguint().unwrap();
    let mut truncated = truncate_biguint_to_u32(&abs);
    if was_negative {
        truncated.wrapping_neg()
    } else {
        truncated
    }
}

Demo

You may use truncate_bigint_to_u32(a) as i32 if you need a signed number.


There is also a to_signed_bytes_le() method with which you could extract the bytes and decode that into a primitive integer directly:

fn truncate_bigint_to_u32_slow(a: &BigInt) -> u32 {
    let mut bytes = a.to_signed_bytes_le();
    bytes.resize(4, 0);
    bytes[0] as u32 | (bytes[1] as u32) << 8 | (bytes[2] as u32) << 16 | (bytes[3] as u32) << 24
}

This method is extremely slow compared to the above methods and I don't recommend using it.

Upvotes: 8

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382354

There's no natural truncation of a big integer into a smaller one. Either it fits or you have to decide what value you want.

You could do this:

println!("Truncation of {} is {:?}.", num1, num1.to_i32().unwrap_or(-1));

or

println!("Truncation of {} is {:?}.", num1, num1.to_i32().unwrap_or(std::i32::MAX));

but your application logic should probably dictate what's the desired behavior when the returned option contains no value.

Upvotes: 4

Related Questions