Skip to content

Commit 99153ac

Browse files
committedJul 22, 2018
add std.math.big.Int.fitsInTwosComp
so that we can pass runtime-known values
·
0.15.20.3.0
1 parent d53fae3 commit 99153ac

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed
 

‎std/math/big/int.zig‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,20 @@ pub const Int = struct {
150150
return bits;
151151
}
152152

153-
pub fn fits(self: Int, comptime T: type) bool {
153+
pub fn fitsInTwosComp(self: Int, is_signed: bool, bit_count: usize) bool {
154154
if (self.eqZero()) {
155155
return true;
156156
}
157-
if (!T.is_signed and !self.positive) {
157+
if (!is_signed and !self.positive) {
158158
return false;
159159
}
160160

161-
const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and T.is_signed);
162-
return T.bit_count >= req_bits;
161+
const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and is_signed);
162+
return bit_count >= req_bits;
163+
}
164+
165+
pub fn fits(self: Int, comptime T: type) bool {
166+
return self.fitsInTwosComp(T.is_signed, T.bit_count);
163167
}
164168

165169
// Returns the approximate size of the integer in the given base. Negative values accomodate for

0 commit comments

Comments
 (0)
Please sign in to comment.