当前位置: 首页 > news >正文

【unitrix】 3.2 位取反运算(not.rs)

一、源码

这段代码是用Rust语言实现的一个类型系统级别的位取反运算(!运算符),主要用于处理二进制数的位运算。代码使用了类型级别的编程技术,通过Rust的特性(trait)系统来实现。

use core::ops::Not;
use crate::number::{Z0, P1, N1, B0, B1, NonNegOne, NonZero, Var, PrimitiveInt};// ==================== 位取反运算 (!运算符) ====================// ========== 基础类型实现 ==========// 零取反:!0 = -1
impl Not for Z0 {type Output = N1;fn not(self) -> Self::Output {N1}
}// 正一取反:!1 = -2 (二进制表示为 B0<N1>)
impl Not for P1 {type Output = B0<N1>;fn not(self) -> Self::Output {B0::new()}
}// 负一取反:!(-1) = 0
impl Not for N1 {type Output = Z0;fn not(self) -> Self::Output {Z0}
}// ========== 递归类型实现 ==========// 二进制0前缀取反:!B0<H> = B1<!H>
impl<H: NonZero + NonNegOne + Not> Not for B0<H> {type Output = B1<H::Output>;fn not(self) -> Self::Output {B1::new()}
}// 二进制1前缀取反:!B1<H> = B0<!H>
impl<H: NonZero + Not> Not for B1<H> {type Output = B0<H::Output>;fn not(self) -> Self::Output {B0::new()}
}// ========== 特殊处理 ==========// -2(B0<N1>)取反特化为1
impl Not for B0<N1> {type Output = P1;fn not(self) -> Self::Output {P1}
}// 注:浮点类型未实现Not(位取反对浮点数无意义)// 变量类型取反:!Var<T> = Var<!T>
impl<T: PrimitiveInt + Not> Not for Var<T> {type Output = Var<<T as Not>::Output>;#[inline(always)]fn not(self) -> Self::Output {Var(!self.0)}
}

二、代码分析

  1. 导入依赖
use core::ops::Not;
use crate::number::{Z0, P1, N1, B0, B1, NonNegOne, NonZero, Var, PrimitiveInt};
  • Not trait:这是Rust标准库中的位取反运算符trait。

  • 其他类型(Z0, P1, N1, B0, B1, Var等)是自定义的类型,用于表示不同的数字或二进制位。

  1. 基础类型实现
    这部分实现了基本数字类型的位取反运算。
a. 零取反
impl Not for Z0 {type Output = N1;fn not(self) -> Self::Output {N1}
}
  • Z0表示数字0。

  • 位取反运算!0的结果是-1(用N1表示)。

b. 正一取反
impl Not for P1 {type Output = B0<N1>;fn not(self) -> Self::Output {B0::new()}
}
  • P1表示数字1。

  • 位取反运算!1的结果是-2(用B0表示,即二进制10)。

c. 负一取反
impl Not for N1 {type Output = Z0;fn not(self) -> Self::Output {Z0}
}
  • N1表示数字-1。

  • 位取反运算!(-1)的结果是0(用Z0表示)。

  1. 递归类型实现
    这部分处理二进制数的位取反,通过递归的方式处理更高位的二进制数。
a. 二进制0前缀取反
impl<H: NonZero + NonNegOne + Not> Not for B0<H> {type Output = B1<H::Output>;fn not(self) -> Self::Output {B1::new()}
}
  • B0表示一个以0为前缀的二进制数(例如B0表示10,即-2)。

  • 取反后变为B1<!H>(即前缀0变为1,剩余部分取反)。

b. 二进制1前缀取反
impl<H: NonZero + Not> Not for B1<H> {type Output = B0<H::Output>;fn not(self) -> Self::Output {B0::new()}
}
  • B1表示一个以1为前缀的二进制数(例如B1表示01,即1)。

  • 取反后变为B0<!H>(即前缀1变为0,剩余部分取反)。

  1. 特殊处理
impl Not for B0<N1> {type Output = P1;fn not(self) -> Self::Output {P1}
}
  • B0表示10(即-2)。

  • 直接特化为!(-2) = 1(用P1表示)。

  1. 变量类型取反
impl<T: PrimitiveInt + Not> Not for Var<T> {type Output = Var<<T as Not>::Output>;#[inline(always)]fn not(self) -> Self::Output {Var(!self.0)}
}
  • Var是一个包装类型,用于表示变量。

  • 如果T实现了Not,则Var也可以进行位取反运算,结果为Var<!T>。

三、总结

这段代码实现了一个类型级别的位取反运算系统,支持:

  1. 基本数字(0、1、-1)的位取反。

  2. 二进制数的递归位取反(B0和B1)。

  3. 特殊情况的优化(B0直接映射为1)。

  4. 变量类型的位取反。

这种技术常用于类型安全的嵌入式DSL(领域特定语言)或数学库中,确保编译期就能完成某些计算或检查。

http://www.lryc.cn/news/572426.html

相关文章:

  • 【数字人开发】Unity+百度智能云平台实现长短文本个性化语音生成功能
  • 吃透 Golang 基础:Goroutine
  • golang excel导出时需要显示刷新
  • Set_path_margin 命令介绍
  • C++中所有数据类型
  • Jenkins通过Pipeline流水线方式编译Java项目
  • Docker+Jenkins+git实现Golang项目自动部署
  • springboot 打的可执行jar包单独更新某个jar包
  • JMeter 高阶玩法:分布式压测的技术核心技术要点
  • 【K8S】详解NodePort 和 ClusterIP
  • 大数据复习HDFS
  • 11.0592MHz晶振:电脑主板的“心脏”
  • 通过Docker挂载nginx并修改页面
  • ros中相机话题在web页面上的显示,尝试js解析sensor_msgs/Image数据
  • 嵌入式开发之freeRTOS移植
  • Kafka性能调优全攻略:从JVM参数到系统优化
  • Java的SpringAI+Deepseek大模型实战
  • 基于keepalived、vip实现高可用nginx (centos)
  • mongodb单节点改副本集模式
  • Python 开发环境全栈隔离架构:从 Anaconda 到 PyCharm 的四级防护体系
  • NetworkManager介绍与用法
  • 【单片机】51单片机练习代码
  • GRBL_UNO R3编译下载
  • Spring Boot 虚拟线程 vs WebFlux:谁更胜一筹?
  • Spring-创建第一个SpringBoot项目
  • apisix-使用hmac-auth插件进行接口签名身份验证\apisix consumer
  • SpringBoot项目启动时自动加载数据到Redis的完整实现方案,用于存储字典,定时任务,登录用户等
  • Spring @Autowired 依赖注入全解析
  • 语音情感识别:CNN-LSTM 和注意力增强 CNN-LSTM 模型的比较分析
  • Hive优化详细讲解