【unitrix数间混合计算】2.21 二进制整数加法计算(bin_add.rs)

一、源码

这段代码是用Rust实现的类型级二进制整数加法运算,通过类型系统在编译期完成计算。它定义了一系列trait实现来处理不同类型的二进制数相加的情况。

rust 复制代码
use core::ops::Add;
use crate::number::{
    Z0, O, I, Bin, Equal, Negative,
    BinInt, BinPos, BinUnsigned, Bit,
    AddOne, BinSignedOpWith, Cmp,
};

// 零相关加法
impl<Int: BinInt> Add<Int> for Z0 {
    type Output = Int;
    #[inline] fn add(self, rhs: Int) -> Self::Output { rhs }
}

impl<U: BinUnsigned, B: Bit> Add<Z0> for Bin<U, B> where Self: BinPos {
    type Output = Self;
    #[inline] fn add(self, _: Z0) -> Self::Output { self }
}

impl<P: BinPos> Add<Z0> for Negative<P> {
    type Output = Self;
    #[inline] fn add(self, _: Z0) -> Self::Output { self }
}

// 非负数加比特位
impl Add<O> for Z0 {
    type Output = Z0;
    #[inline] fn add(self, _: O) -> Self::Output { Z0 }
}

impl Add<I> for Z0 {
    type Output = Bin<Z0, I>;
    #[inline] fn add(self, _: I) -> Self::Output { Bin::default() }
}

impl<U: BinUnsigned, B: Bit> Add<O> for Bin<U, B> where Bin<U, B>: BinPos {
    type Output = Bin<U, B>;
    #[inline] fn add(self, _: O) -> Self::Output { Bin::default() }
}

impl<U: BinUnsigned, B: Bit> Add<I> for Bin<U, B> where Self: AddOne {
    type Output = Self::Output;
    #[inline] fn add(self, _: I) -> Self::Output { Bin::default() }
}

// 正数间加法
impl<Ul: BinUnsigned, Ur: BinUnsigned, B: Bit> Add<Bin<Ur, O>> for Bin<Ul, B> where Ul: Add<Ur> {
    type Output = Bin<<Ul as Add<Ur>>::Output, B>;
    #[inline] fn add(self, _: Bin<Ur, O>) -> Self::Output { Bin::default() }
}

impl<Ul: BinUnsigned, Ur: BinUnsigned> Add<Bin<Ur, I>> for Bin<Ul, O> where Ul: Add<Ur> {
    type Output = Bin<<Ul as Add<Ur>>::Output, I>;
    #[inline] fn add(self, _: Bin<Ur, I>) -> Self::Output { Bin::default() }
}

impl<Ul: BinUnsigned, Ur: BinUnsigned> Add<Bin<Ur, I>> for Bin<Ul, I> 
where Ul: Add<Ur>, <Ul as Add<Ur>>::Output: AddOne {
    type Output = Bin<<<Ul as Add<Ur>>::Output as AddOne>::Output, O>;
    #[inline] fn add(self, _: Bin<Ur, I>) -> Self::Output { Bin::default() }
}

// 负数相关加法
impl<Pl: BinPos, Pr: BinPos> Add<Negative<Pr>> for Negative<Pl> where Pl: Add<Pr>, <Pl as Add<Pr>>::Output: BinPos {
    type Output = Negative<<Pl as Add<Pr>>::Output>;
    #[inline] fn add(self, _: Negative<Pr>) -> Self::Output { Negative::default() }
}

impl<Ul: BinUnsigned, Bl: Bit, Pr: BinPos> Add<Negative<Pr>> for Bin<Ul, Bl> 
where Self: BinPos, Equal: Cmp<Self, Pr>, <Equal as Cmp<Self, Pr>>::Output: BinSignedOpWith<Self, Pr> {
    type Output = <<Equal as Cmp<Self, Pr>>::Output as BinSignedOpWith<Self, Pr>>::Output;
    #[inline] fn add(self, _: Negative<Pr>) -> Self::Output { Default::default() }
}

impl<Ul: BinPos, Ur: BinPos> Add<Ur> for Negative<Ul> 
where Equal: Cmp<Ur, Ul>, <Equal as Cmp<Ur, Ul>>::Output: BinSignedOpWith<Ur, Ul> {
    type Output = <<Equal as Cmp<Ur, Ul>>::Output as BinSignedOpWith<Ur, Ul>>::Output;
    #[inline] fn add(self, _: Ur) -> Self::Output { Default::default() }
}

二、基本类型说明

  • Z0: 表示数字0,也是数的终止标志

  • O和I: 表示二进制位的0和1

  • Bin<U, B>: 表示二进制数,U是高位部分,B是最低位(0或1)

  • Negative

    : 表示负数,P是其绝对值对应的正数

三、零相关加法

rust 复制代码
impl<Int: BinInt> Add<Int> for Z0 {
    type Output = Int;
    fn add(self, rhs: Int) -> Self::Output { rhs }
}

任何数加0等于它自己,0加任何数也等于它自己。

四、非负数加比特位

处理0加二进制位0、0加二进制位1的情况,以及正数加二进制位0、加二进制位1的情况:

rust 复制代码
impl Add<I> for Z0 {
    type Output = Bin<Z0, I>;  // 0 + 1 = 1
    fn add(self, _: I) -> Self::Output { Bin::default() }
}

五、正数间加法

处理两个正二进制数相加的情况,分为三种情况:

  1. 任意二进制数加偶数(最低位不变)

  2. 偶数加奇数(最低位为1)

  3. 奇数加奇数(需要进位)

例如:

rust 复制代码
impl<Ul: BinUnsigned, Ur: BinUnsigned, B: Bit> Add<Bin<Ur, O>> for Bin<Ul, B> where Ul: Add<Ur> {
    type Output = Bin<<Ul as Add<Ur>>::Output, B>;
    // 例如: 11(Bin<Bin<Z0,I>,I>) + 10(Bin<Bin<Z0,I>,O>) = 101(Bin<Bin<Bin<Z0,I>,O>,I>)
}

六、负数相关加法

处理负数相加,以及正负数相加的情况:

rust 复制代码
impl<Pl: BinPos, Pr: BinPos> Add<Negative<Pr>> for Negative<Pl> where Pl: Add<Pr>, <Pl as Add<Pr>>::Output: BinPos {
    type Output = Negative<<Pl as Add<Pr>>::Output>;
    // 例如: -3 + -2 = -5
}

impl<Ul: BinPos, Ur: BinPos> Add<Ur> for Negative<Ul> 
where Equal: Cmp<Ur, Ul>, <Equal as Cmp<Ur, Ul>>::Output: BinSignedOpWith<Ur, Ul> {
    type Output = <<Equal as Cmp<Ur, Ul>>::Output as BinSignedOpWith<Ur, Ul>>::Output;
    // 例如: -3 + 5 = 2
}

七、特点

  1. 所有计算都在类型系统层面完成,运行时没有任何计算

  2. 使用Default::default()作为占位符,实际值由类型系统决定

  3. 通过trait约束确保类型安全

  4. 支持正数、负数和零的加法运算

这种类型级编程技术在需要编译期计算的场景中非常有用,如模板元编程、维度检查等。

相关推荐
Include everything4 小时前
Rust学习笔记(二)|变量、函数与控制流
笔记·学习·rust
Source.Liu7 小时前
【unitrix数间混合计算】2.20 比较计算(cmp.rs)
rust
许野平7 小时前
Rust:构造函数 new() 如何进行错误处理?
开发语言·后端·rust
许野平7 小时前
Rust:专业级错误处理工具 thiserror 详解
rust·error·错误处理·result·thiserror
蒋星熠12 小时前
Rust 异步生态实战:Tokio 调度、Pin/Unpin 与零拷贝 I/O
人工智能·后端·python·深度学习·rust
Include everything12 小时前
Rust学习笔记(一)|Rust初体验 猜数游戏
笔记·学习·rust
华科云商xiao徐1 天前
Rust+Python双核爬虫:高并发采集与智能解析实战
数据库·python·rust
Saya1 天前
Rust 命名模式速查表:15 秒看懂函数在做什么
rust