-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make BigInteger
bit shift and power methods compatible with BigInteger
values
#50336
Comments
Tagging subscribers to this area: @tannergooding, @pgovind Issue DetailsBackground and MotivationThe Proposed APIpublic readonly struct BigInteger : ...
{
...
public static BigInteger operator <<(BigInteger value, BigInteger shift) { ... }
public static BigInteger operator >>(BigInteger value, BigInteger shift) { ... }
public static BigInteger Pow(BigInteger value, BigInteger exponent) { ... }
...
} Usage Examples// Example big integers used in a project centred around big integers
BigInteger bigThree = new BigInteger(3);
BigInteger bigFour = new BigInteger(4);
BigInteger bigOneHundredAndTwelve = new BigInteger(112);
// Bitwise shift by a big integer
BigInteger bigFortyEight = bigThree << bigFour; // == 48, equal to 3 << 4
BigInteger bigSeven = bigOneHundredAndTwelve >> bigFour; // == 7, equal to 112 >> 4
// Big integer raised to a big integer power
BigInteger bigSixtyFour = BigInteger.Power(bigFour, bigThree); // == 64, equal to 4³ Alternative DesignsI'm personally not familiar enough with bit manipulation to propose an implementation, let alone alternative designs to make my suggestion work. I figured it could be useful to open an issue regardless, to discuss the feasibility of this suggestion. RisksSince this suggestion would meddle with bitwise operations, I can imagine memory management would get rather messy very quickly. Although it's theoretically already possible to create unwieldy
|
Can you share your scenario in which you need to shift by more than two billion bits? |
This is actually impossible, lol. The rhs of a shift operator overload must be int |
I don't think there is a scenario where we need to represent a value larger than Likewise, C# requires the RHS of a user-defined shift operator to be It is not uncommon, for example, to have The same could be said for BigInteger. That is, you may have done some complex set of calculations and what you ultimately have is a positive integer that is far less than 2 billion and is therefore "in range", but due to limitations of the language you are forced to downcast to There are proposals such as dotnet/csharplang#4431 that discuss expanding the C# limitations here and I recently raised it to Mads as part of the static abstracts in interfaces work; particularly as IL and other languages do not have this limitation. |
I agree that representing a number greater than I wasn't aware that bit shifts only worked with integer values, that was poor research on my part. Should I close this issue and create a new one just for the the |
Closing this as unactionable unless the language takes dotnet/csharplang#4666 If the language does add support, we should be able to reopen and consider this. |
Background and Motivation
The
BigInteger
struct is very useful to perform arithmetic operations beyond the traditionalint
andlong
ranges, but I've noticed that the bitwise shift and integer power methods do not accept aBigInteger
value, which seems rather inconsistent. The strength of the struct lies in its boundlessness, so restricting bit shifts and powers ofBigInteger
values to just integers seems to be a missed opportunity.Proposed API
Usage Examples
Alternative Designs
I'm personally not familiar enough with bit manipulation to propose an implementation, let alone alternative designs to make my suggestion work. I figured it could be useful to open an issue regardless, to discuss the feasibility of this suggestion.
Risks
Since this suggestion would meddle with bitwise operations, I can imagine memory management would get rather messy very quickly. Although it's theoretically already possible to create unwieldy
BigInteger
values with just theint
based methods, manipulating theBigInteger
's nitty gritty directly seems more error-prone.The text was updated successfully, but these errors were encountered: