summaryrefslogtreecommitdiffstats
path: root/core/bitArray.js
diff options
context:
space:
mode:
Diffstat (limited to 'core/bitArray.js')
-rw-r--r--core/bitArray.js23
1 files changed, 22 insertions, 1 deletions
diff --git a/core/bitArray.js b/core/bitArray.js
index 40e4acd..a6d0617 100644
--- a/core/bitArray.js
+++ b/core/bitArray.js
@@ -31,7 +31,7 @@
sjcl.bitArray = {
/**
* Array slices in units of bits.
- * @param {bitArray a} The array to slice.
+ * @param {bitArray} a The array to slice.
* @param {Number} bstart The offset to the start of the slice, in bits.
* @param {Number} bend The offset to the end of the slice, in bits. If this is undefined,
* slice until the end of the array.
@@ -43,6 +43,27 @@ sjcl.bitArray = {
},
/**
+ * Extract a number packed into a bit array.
+ * @param {bitArray} a The array to slice.
+ * @param {Number} bstart The offset to the start of the slice, in bits.
+ * @param {Number} length The length of the number to extract.
+ * @return {Number} The requested slice.
+ */
+ extract: function(a, bstart, blength) {
+ // FIXME: this Math.floor is not necessary at all, but for some reason
+ // seems to suppress a bug in the Chromium JIT.
+ var x, sh = Math.floor((-bstart-blength) & 31);
+ if ((bstart + blength - 1 ^ bstart) & -32) {
+ // it crosses a boundary
+ x = (a[bstart/32|0] << (32 - sh)) ^ (a[bstart/32+1|0] >>> sh);
+ } else {
+ // within a single word
+ x = a[bstart/32|0] >>> sh;
+ }
+ return x & ((1<<blength) - 1);
+ },
+
+ /**
* Concatenate two bit arrays.
* @param {bitArray} a1 The first array.
* @param {bitArray} a2 The second array.