Giter Club home page Giter Club logo

Comments (5)

cuviper avatar cuviper commented on August 30, 2024

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.

yorickpeterse avatar yorickpeterse commented on August 30, 2024

@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.

cuviper avatar cuviper commented on August 30, 2024

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.

yorickpeterse avatar yorickpeterse commented on August 30, 2024

@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.

linclelinkpart5 avatar linclelinkpart5 commented on August 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.