mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
math.unsigned: fix lsh() for Uint128, add tests (#24840)
This commit is contained in:
parent
7e35d40661
commit
2a5398aff6
2 changed files with 19 additions and 1 deletions
|
@ -300,7 +300,16 @@ pub fn (u Uint128) quo_rem(v Uint128) (Uint128, Uint128) {
|
|||
// lsh returns u << n
|
||||
pub fn (u Uint128) lsh(n u32) Uint128 {
|
||||
mut s := Uint128{}
|
||||
if n > 64 {
|
||||
if n == 0 {
|
||||
s.lo = u.lo
|
||||
s.hi = u.hi
|
||||
} else if n >= 128 {
|
||||
s.lo = 0
|
||||
s.hi = 0
|
||||
} else if n == 64 {
|
||||
s.lo = 0
|
||||
s.hi = u.lo
|
||||
} else if n > 64 {
|
||||
s.lo = 0
|
||||
s.hi = u.lo << (n - 64)
|
||||
} else {
|
||||
|
|
|
@ -174,3 +174,12 @@ fn test_separators() {
|
|||
fn test_new() {
|
||||
assert unsigned.uint128_new(max_u64, max_u64) == unsigned.uint128_max
|
||||
}
|
||||
|
||||
fn test_lsh() {
|
||||
a := unsigned.uint128_from_dec_str('123456789012345678901234567890')!
|
||||
assert a.str() == a.lsh(0).str()
|
||||
assert '246913578024691357802469135780' == a.lsh(1).str()
|
||||
assert '259801135457060040952792416454273138688' == a.lsh(64).str()
|
||||
assert '302984417681386893975453667670529933312' == a.lsh(100).str()
|
||||
assert unsigned.uint128_zero == a.lsh(200)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue