How i128 are stored in a 32 bit OS architecture?

Hi all,

How a program like the one below works in a 32 bit OS architecture:

fn main() {
    let i = std::i128::MAX;
    print!("{:?}", i);
}

How i128 are stored? Does rust-compiler do something?
Simple question but not so obvious to me.

Thank you,
JP

TL;DR they are stored on the stask as seperate 32-bit values

Ok thanks. And what does:

?

I am not familiar with the left shift operator!

You can name the operator, why can't you google it? :wink:

http://www.c4learn.com/c-programming/c-bitwise-left-shift-operator/

Probably by laziness ;-). I read the rust doc and I understood nothing. The links that you provided was clearer.

This operator is quite useful for bit calculation... I can even easily invert the operation you gave:

fn bit_split(to_split: u128, bit: u32, stack: &mut Vec<u128>) {
   let mut max_size_bit: u128 = 0;
   for i in 0..bit {
       max_size_bit = max_size_bit | 1 << i;
   }
   stack.push(to_split & max_size_bit);
   let next_to_split = to_split >> bit;
   if to_split >> bit != 0 {
       bit_split(next_to_split, bit, stack)
   }
}

For example bit_split(8, 1, &mut stack) give a stack of 8 splited in 1 bit ( [0, 0, 0, 1])

I have 1 more question linked:
How performances are impacted when you store a u128 in a u32 stack? what the idiotic way to choose the size in rust? using "usize" and "isize"?

1 Like

I presume you mean idiomatic? There are unlimited idiotic ways to do things in any programming language.

Use usize for counting things that are in memory. Otherwise use the right size for whatever you are doing. Don't use u32 to track the U.S. national debt, but it's fine for counting the eggs in most recipes.

5 Likes