MCPcopy
hub / github.com/pquerna/ffjson / rightShift

Function rightShift

fflib/v1/decimal.go:109–158  ·  view source on GitHub ↗

Binary shift right (* 2) by k bits. k <= maxShift to avoid overflow.

(a *decimal, k uint)

Source from the content-addressed store, hash-verified

107
108// Binary shift right (* 2) by k bits. k <= maxShift to avoid overflow.
109func rightShift(a *decimal, k uint) {
110 r := 0 // read pointer
111 w := 0 // write pointer
112
113 // Pick up enough leading digits to cover first shift.
114 n := 0
115 for ; n>>k == 0; r++ {
116 if r >= a.nd {
117 if n == 0 {
118 // a == 0; shouldn't get here, but handle anyway.
119 a.nd = 0
120 return
121 }
122 for n>>k == 0 {
123 n = n * 10
124 r++
125 }
126 break
127 }
128 c := int(a.d[r])
129 n = n*10 + c - '0'
130 }
131 a.dp -= r - 1
132
133 // Pick up a digit, put down a digit.
134 for ; r < a.nd; r++ {
135 c := int(a.d[r])
136 dig := n >> k
137 n -= dig << k
138 a.d[w] = byte(dig + '0')
139 w++
140 n = n*10 + c - '0'
141 }
142
143 // Put down extra digits.
144 for n > 0 {
145 dig := n >> k
146 n -= dig << k
147 if w < len(a.d) {
148 a.d[w] = byte(dig + '0')
149 w++
150 } else if dig > 0 {
151 a.trunc = true
152 }
153 n = n * 10
154 }
155
156 a.nd = w
157 trim(a)
158}
159
160// Cheat sheet for left shift: table indexed by shift count giving
161// number of new digits that will be introduced by that shift.

Callers 1

ShiftMethod · 0.70

Calls 1

trimFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…