MakeBitArrayFromInt64 creates a bit array with the specified size. The bits from the integer are written to the right of the bit array and the sign bit is extended.
(bitLen uint, val int64, valWidth uint)
| 158 | // size. The bits from the integer are written to the right of the bit |
| 159 | // array and the sign bit is extended. |
| 160 | func MakeBitArrayFromInt64(bitLen uint, val int64, valWidth uint) BitArray { |
| 161 | if bitLen == 0 { |
| 162 | return BitArray{} |
| 163 | } |
| 164 | d := MakeZeroBitArray(bitLen) |
| 165 | if bitLen < valWidth { |
| 166 | // Fast path, no sign extension to compute. |
| 167 | d.words[len(d.words)-1] = word(val << (numBitsPerWord - bitLen)) |
| 168 | return d |
| 169 | } |
| 170 | if val&(1<<(valWidth-1)) != 0 { |
| 171 | // Sign extend, fill ones in every word but the last. |
| 172 | for i := 0; i < len(d.words)-1; i++ { |
| 173 | d.words[i] = ^word(0) |
| 174 | } |
| 175 | } |
| 176 | // Shift the value to its given number of bits, to position the sign |
| 177 | // bit to the left. |
| 178 | val = val << (numBitsPerWord - valWidth) |
| 179 | // Shift right back with arithmetic shift to extend the sign bit. |
| 180 | val = val >> (numBitsPerWord - valWidth) |
| 181 | // Store the right part of the value in the last word. |
| 182 | d.words[len(d.words)-1] = word(val << (numBitsPerWord - d.lastBitsUsed)) |
| 183 | // Store the left part in the next-to-last word, if any. |
| 184 | if valWidth > uint(d.lastBitsUsed) { |
| 185 | d.words[len(d.words)-2] = word(val >> d.lastBitsUsed) |
| 186 | } |
| 187 | return d |
| 188 | } |
| 189 | |
| 190 | // AsInt64 returns the int constituted from the rightmost bits in the |
| 191 | // bit array. |
no test coverage detected
searching dependent graphs…