summaryrefslogtreecommitdiff
path: root/common/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/util.c')
-rw-r--r--common/util.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/common/util.c b/common/util.c
index 7ed4c45c53..deaa62f37f 100644
--- a/common/util.c
+++ b/common/util.c
@@ -767,3 +767,27 @@ int binary_first_base3_from_bits(int *bits, int nbits)
/* binary_below is normal binary system value if !has_z. */
return binary_below;
}
+
+int binary_from_bits(int *bits, int nbits)
+{
+ int value = 0;
+ int i;
+
+ /* Loop through every binary digit, from MSB to LSB. */
+ for (i = nbits - 1; i >= 0; i--)
+ value = (value << 1) | bits[i];
+
+ return value;
+}
+
+int ternary_from_bits(int *bits, int nbits)
+{
+ int value = 0;
+ int i;
+
+ /* Loop through every ternary digit, from MSB to LSB. */
+ for (i = nbits - 1; i >= 0; i--)
+ value = (value * 3) + bits[i];
+
+ return value;
+}