Comments (5)
It's not obvious how these operators should work with negative numbers without specifying their representation. Currently we store BigInt
as sign+magnitude, but I don't want to be tied to that.
Ideally, I think we'd want this to behave similarly to the primitive integers, so we don't have surprises like Shr
in #1. So that means emulating 2's-complement behavior, regardless of the actual representation. We can treat them as having an infinite prefix of 0 or 1 bits for positive and negative, like Python does.
We also don't support Not
yet, but we could for BigInt
with this "infinite sign" semantic.
from num-bigint.
@cuviper If it helps: the reason for wanting this feature is that I'm working on a VM for a programming language. Said language exposes an integer type that under the hoods may or may not be a bigint, much like Ruby and Python do. I'd like to be able to perform operations such as |
and &
on those integers, regardless of whether they're an i64
, BigInt
, etc.
Looking at https://github.com/ruby/ruby/blob/f92924923dd4d707abc8bf431d3c3e746fd8515a/bignum.c#L6417 it seems Ruby may emulate 2's-complement behaviour, but I can't tell for certain.
Regarding the implementation for num-bigint, is there a place where this is done in a similar fashion that can perhaps be adopted for BitAnd & friends? I'm happy to provide a PR but I'm not very familiar with the internals of BigInt/BigUint so having something as a reference would be useful.
from num-bigint.
I don't have a directly applicable example for you. Most of the time, the representation doesn't really matter. The fix in #8 is sort of related, but I don't think it's similar enough to help here.
Currently BigInt
is just a sign and magnitude. The naive approach to these would be to just do a 2's-complement conversion on the magnitude of negative operands -- padded with sufficiently "infinite" sign bits, then perform the op, and finally convert 2's-complement again if the result should be negative. I suspect there's probably a more direct way to compute each op for the same result, but I haven't tried to work those details out yet.
FWIW, the "help wanted" label doesn't mean that I won't do it myself, just that I'm not working on it right now, so anyone else is welcome to try. If you think you can work it out, great! But don't worry about it if you can't.
from num-bigint.
@cuviper I took a look around but I'm afraid this will be a bit out of my league. Would it at least help if I were to submit a PR adding some tests? That way at least some of the work is taken care of ๐
from num-bigint.
I'd love to have this feature as well! Being able to have an integer-like object with "infinite" leading 1s or 0s would help a lot in my use case in my project (bit masking).
from num-bigint.
Related Issues (20)
- Is there a reason that `Copy` isn't implemented for `BigInt` and `BigUint` HOT 3
- `test_convert_f32` test failed on `i686-pc-windows-*` target HOT 8
- Whether to enable simd support๏ผ HOT 2
- Absolute value HOT 6
- Compiling Bigint Without Cargo HOT 1
- Debug output doesn't honor formatting specifier x HOT 1
- to_str_radix produces odd number of digits HOT 2
- Build script could be replaced with const fn HOT 4
- Incorrect handling of "_" chars in decimal part of number HOT 1
- From f64 HOT 2
- BigInt absolute value as a BigUint value HOT 2
- Add is_negative and is_non_negative HOT 1
- BigInt::parse_bytes converting a string with '_' as bigint with base 36 which isn't expected. HOT 2
- Feature Request: Ref type for {BigInt|BigUint} HOT 2
- Ability to create const BigUint etc. HOT 2
- Add powi method for `Sign` enum HOT 1
- re-export num_traits HOT 1
- Possible Incompleteness HOT 2
- Mixed-algorithm GCD (potential optimization) HOT 1
- x86_64-unknown-linux-gnux32 no longer works with 0.4.5 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from num-bigint.