How does Rust's 128-bit integer `i128` work on a 64-bit system?

前端 未结 4 760
北荒
北荒 2021-01-30 19:16

Rust has 128-bit integers, these are denoted with the data type i128 (and u128 for unsigned ints):

let a: i128 = 1701411834604692317316         


        
4条回答
  •  佛祖请我去吃肉
    2021-01-30 19:51

    Yes, just the same way as 64-bit integers on 32-bit machines were handled, or 32-bit integers on 16-bit machines, or even 16- and 32-bit integers on 8-bit machines (still applicable to microcontrollers!). Yes, you store the number in two registers, or memory locations, or whatever (it doesn't really matter). Addition and subtraction are trivial, taking two instructions and using the carry flag. Multiplication requires three multiplies and some additions (it's common for 64-bit chips to already have a 64x64->128 multiply operation that outputs to two registers). Division... requires a subroutine and is quite slow (except in some cases where division by a constant can be transformed into a shift or a multiply), but it still works. Bitwise and/or/xor merely have to be done on the top and bottom halves separately. Shifts can be accomplished with rotation and masking. And that pretty much covers things.

提交回复
热议问题