| Crates.io | balanced-ternary |
| lib.rs | balanced-ternary |
| version | 2.1.0 |
| created_at | 2025-02-19 02:48:22.544149+00 |
| updated_at | 2025-06-09 17:05:08.702382+00 |
| description | A library to manipulate balanced ternary values. |
| homepage | |
| repository | https://github.com/Trehinos/balanced-ternary |
| max_upload_size | |
| id | 1560864 |
| size | 156,599 |
Balanced Ternary is a Rust library for manipulating
balanced ternary
numbers, a numeral system with digits -1, 0, and +1.
This system is particularly useful in specialized computing applications such as reversible computing, digital signal processing, and three-valued logic modeling.
#![no_std] environments.<<, >>).+, 0, and - symbols by default, or custom ones.featuresAll features are enabled by default.
To enable only some features, use the default-features option
in your dependency declaration:
[dependencies.balanced-ternary]
version = "*.*"
default-features = false
# Choose which one to enable
features = ["ternary-string", "tryte", "ternary-store"]
Without any feature, this library provide the type Digit and all its operations and the trait DigitOperate.
ternary-stringAdd the structure Ternary which is a vector of Digits and a lot of utilities
to manipulate digits into the ternary number.
Implements DigitOperate.
tryteNeeds the feature
ternary-string.
Add the type Tryte<N> which is a fixed size copy-type ternary number.
Implements DigitOperate.
ternary-storeNeeds the feature
ternary-string.
Add structures to store ternaries efficiently. These types are provided:
DataTernary: a variable length ternary number stored into TritsChunks,TritsChunk: a fixed size copy-type 5 digits stored into one byte,Ter40: a fixed size copy-type 40 digits stored into one 64 bits integer. Implements DigitOperate.The library supports numerous three-valued logic operations, each of them having its own specificities:
The library provides a variety of operations that can be performed on individual balanced ternary digits. These operations include logical operations, arithmetic operations, and utility functions that are useful for manipulating ternary numbers at the digit level. Below are the truth table of these operations:

You can use these operations with the DigitOperate trait methods,
each_* (with, zip, zip_carry):
#[cfg(feature = "ternary-string")]
fn test_each() {
use crate::*;
let ternary = Ternary::parse("+0-");
assert_eq!(ternary.each(Digit::possibly).to_string(), "++-");
}
use balanced_ternary::*;
fn test() {
let ternary = Ternary::from_dec(5);
assert_eq!(ternary.to_string(), "+--");
let ternary = Ternary::parse("+--");
assert_eq!(ternary.to_dec(), 5);
let ternary = "+-0".parse::<Ternary>().unwrap();
assert_eq!(ternary.to_string(), "+-0");
}
use balanced_ternary::*;
fn test() {
let a = Ternary::from_dec(9);
let b = Ternary::from_dec(4);
let sum = &a + &b;
assert_eq!(sum.to_string(), "+++");
assert_eq!(sum.to_dec(), 13);
let bitwise = &Ternary::parse("++00") & &Ternary::parse("0000");
assert_eq!(bitwise.to_string(), "0000");
let bitwise = &Ternary::parse("++00") & &Ternary::parse("0+00");
assert_eq!(bitwise.to_string(), "0+00");
let bitwise = &Ternary::parse("+000") | &Ternary::parse("000-");
assert_eq!(bitwise.to_string(), "+000");
let bitwise = &Ternary::parse("+000") & &Ternary::parse("000-");
assert_eq!(bitwise.to_string(), "000-");
let bitwise = &Ternary::parse("+000") | &Ternary::parse("000+");
assert_eq!(bitwise.to_string(), "+00+");
let shifted = &Ternary::parse("+0-") << 2;
assert_eq!(shifted.to_string(), "+0-00");
let back = &shifted >> 2;
assert_eq!(back.to_string(), "+0-");
}
use balanced_ternary::*;
fn test() {
let negative = Ternary::from_dec(-5);
assert_eq!(negative.to_string(), "-++");
}
use balanced_ternary::*;
fn test() {
let ternary = Ternary::parse("+0-");
// Using `.iter()`
let v: Vec<Digit> = ternary.iter().cloned().collect();
assert_eq!(v, vec![Pos, Zero, Neg]);
// Using `IntoIterator`
let v: Vec<Digit> = Ternary::parse("+0-").into_iter().collect();
assert_eq!(v, vec![Pos, Zero, Neg]);
}
The complete API documentation can be found on docs.rs. There you can find descriptions and examples of available types and methods.
Copyright (c) 2025 Sébastien GELDREICH
Balanced Ternary is licensed under the MIT License.