File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -323,9 +323,18 @@ pub fn (u Uint128) lsh(n u32) Uint128 {
323
323
// rsh returns u >> n
324
324
pub fn (u Uint128) rsh (n u32 ) Uint128 {
325
325
mut s := Uint128 {}
326
- if n > 64 {
326
+ if n == 0 {
327
+ s.lo = u.lo
328
+ s.hi = u.hi
329
+ } else if n > = 128 {
330
+ s.lo = 0
331
+ s.hi = 0
332
+ } else if n == 64 {
333
+ s.hi = 0
334
+ s.lo = u.hi
335
+ } else if n > 64 {
327
336
s.hi = 0
328
- s.lo = u.hi << (n - 64 )
337
+ s.lo = u.hi >> (n - 64 )
329
338
} else {
330
339
s.lo = u.lo >> n | u.hi << (64 - n)
331
340
s.hi = u.hi >> n
Original file line number Diff line number Diff line change @@ -183,3 +183,13 @@ fn test_lsh() {
183
183
assert '302984417681386893975453667670529933312' == a.lsh (100 ).str ()
184
184
assert unsigned.uint128_zero == a.lsh (200 )
185
185
}
186
+
187
+ fn test_rsh () {
188
+ a := unsigned.uint128_from_dec_str ('279625844435276397900870454226348864638' )!
189
+ assert a.str () == a.rsh (0 ).str ()
190
+ assert '139812922217638198950435227113174432319' == a.rsh (1 ).str ()
191
+ assert '15158547400991018568' == a.rsh (64 ).str ()
192
+ assert '220585896' == a.rsh (100 ).str ()
193
+ assert '1' == a.rsh (127 ).str ()
194
+ assert unsigned.uint128_zero == a.rsh (200 )
195
+ }
You can’t perform that action at this time.
0 commit comments