news 2026/8/6 23:29:43

Bit Operation Algorithm

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Bit Operation Algorithm

项目结构:

/* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : BitMask.cs */ using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.ValueObject { /// <summary> /// 珠宝业务位掩码常量,对位Go bit_mask.go /// </summary> public static class BitMask { #region 材质掩码 public const int GOLD = 1 << 0; public const int KGOLD = 1 << 1; public const int PLATINUM = 1 << 2; public const int DIAMOND = 1 << 3; public const int JADE = 1 << 4; public const int PEARL = 1 << 5; public const int RUBY = 1 << 6; public const int SAPPHIRE = 1 << 7; #endregion #region 风格掩码 public const int LUXURY = 1 << 10; public const int SIMPLE = 1 << 11; public const int RETRO = 1 << 12; public const int MINIMALIST = 1 << 13; #endregion #region 价位档位掩码 public const int LOWPRICE = 1 << 20; public const int MIDPRICE = 1 << 21; public const int HIGHPRICE = 1 << 22; #endregion } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : BlackList.cs */ using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.Entity { /// <summary> /// 黑名单容器 /// </summary> public class BlackList { private readonly HashSet<string> _blackSet = new(); /// <summary> /// 添加黑名单条目 /// </summary> public void AddFake(string id) { _blackSet.Add(id); } /// <summary> /// 判断是否在黑名单 /// </summary> public bool Contains(string id) { return _blackSet.Contains(id); } } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : Filter.cs */ using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.Entity { /// <summary> /// 数据过滤器 /// </summary> public class Filter { public BlackList BlackList { get; set; } = new(); } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : Guide.cs */ using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.Entity { public class Guide { public string GuideID { get; set; } = string.Empty; public int SkillMask { get; set; } public List<string> CustomerPriority { get; set; } = new(); } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : Customer.cs */ using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.Entity { public class Customer { public string CustomerID { get; set; } = string.Empty; public int PreferenceMask { get; set; } public List<string> PriorityList { get; set; } = new(); } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : Jewelry.cs */ using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.Entity { public class Jewelry { public string JewelID { get; set; } = string.Empty; public double Weight { get; set; } public double Price { get; set; } public int MaterialMask { get; set; } } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : MatchDomainRule.cs */ using CSharpAlgorithms.BitOperation.Entity; using CSharpAlgorithms.BitOperation.ValueObject; using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.Domain.Rules { /// <summary> /// 导购客户匹配规则,对位 match_rule.go /// </summary> public class MatchDomainRule { #region 全局固定常量 public const int GUIDE_OFFLINE_MASK = 1 << 30; public const int CUSTOMER_BLACK_MASK = 1 << 31; public const int SENIOR_GUIDE_TAG = 1 << 28; #endregion /// <summary> /// 高奢组合掩码,构造函数初始化 /// </summary> public int LuxuryMask { get; } public MatchDomainRule() { LuxuryMask = BitMask.HIGHPRICE | BitMask.DIAMOND; } /// <summary> /// 校验导购是否合法 /// </summary> public ResultMsg IsGuideValid(Guide? guide) { if (guide is null) return new ResultMsg(false, "实体类型错误,非导购Guide对象"); if (string.IsNullOrEmpty(guide.GuideID)) return new ResultMsg(false, "导购ID不能为空"); if (guide.CustomerPriority.Count == 0) return new ResultMsg(false, "导购未配置客户接待优先级列表,无法参与匹配"); if ((guide.SkillMask & GUIDE_OFFLINE_MASK) != 0) return new ResultMsg(false, $"导购[{guide.GuideID}]当前离岗,禁止分配客户"); return new ResultMsg(true, "导购校验通过"); } /// <summary> /// 校验客户是否合法 /// </summary> public ResultMsg IsCustomerValid(Customer? customer) { if (customer is null) return new ResultMsg(false, "实体类型错误,非客户Customer对象"); if (string.IsNullOrEmpty(customer.CustomerID)) return new ResultMsg(false, "客户ID不能为空"); if (customer.PriorityList.Count == 0) return new ResultMsg(false, "客户未配置导购偏好优先级,无法参与匹配"); if ((customer.PreferenceMask & CUSTOMER_BLACK_MASK) != 0) return new ResultMsg(false, $"客户[{customer.CustomerID}]处于黑名单,禁止分配导购接待"); return new ResultMsg(true, "客户校验通过"); } /// <summary> /// 高奢导购资质校验 /// </summary> public ResultMsg CheckLuxuryMatchLimit(Guide guide, Customer customer) { bool needLuxury = (customer.PreferenceMask & LuxuryMask) != 0; if (!needLuxury) return new ResultMsg(true, "非高奢需求,无导购等级限制"); if ((guide.SkillMask & SENIOR_GUIDE_TAG) == 0) { return new ResultMsg(false, $"客户[{customer.CustomerID}]存在高奢需求,导购[{guide.GuideID}]非资深导购,禁止匹配"); } return new ResultMsg(true, "高奢需求匹配资格校验通过"); } /// <summary> /// 批量准入过滤 /// </summary> public BatchFilterResult BatchCheckMatchEntrance(List<Guide> guides, List<Customer> customers) { List<Guide> validGuide = new(); List<Customer> validCustomer = new(); List<string> errorLog = new(); foreach (var g in guides) { var res = IsGuideValid(g); if (res.Success) validGuide.Add(g); else errorLog.Add($"导购过滤:{res.Message}"); } foreach (var c in customers) { var res = IsCustomerValid(c); if (res.Success) validCustomer.Add(c); else errorLog.Add($"客户过滤:{res.Message}"); } return new BatchFilterResult(validGuide, validCustomer, errorLog); } #region 内部结果封装 /// <summary> /// 单次校验结果 /// </summary> public record ResultMsg(bool Success, string Message); /// <summary> /// 批量过滤结果 /// </summary> public record BatchFilterResult(List<Guide> ValidGuide, List<Customer> ValidCustomer, List<string> ErrorLog); #endregion } } /* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : CombineDomainRule.cs */ using CSharpAlgorithms.BitOperation.ValueObject; using CSharpAlgorithms.BitOperation.Entity; using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.BitOperation.Domain.Rules { /// <summary> /// 首饰礼盒搭配规则 /// </summary> public class CombineDomainRule { public int MaxCombineCount { get; } public double MinCombineWeight { get; } public int MutexGold { get; } public int MutexPlatinum { get; } public CombineDomainRule() { MaxCombineCount = 6; MinCombineWeight = 1.0d; MutexGold = BitMask.GOLD; MutexPlatinum = BitMask.PLATINUM; } /// <summary> /// 单件首饰合法性校验 /// </summary> public MatchDomainRule.ResultMsg IsSingleJewelValid(Jewelry? jewel) { if (jewel is null) return new MatchDomainRule.ResultMsg(false, "对象非首饰Jewelry实体"); if (string.IsNullOrEmpty(jewel.JewelID)) return new MatchDomainRule.ResultMsg(false, "首饰唯一ID为空"); if (jewel.Weight <= 0) return new MatchDomainRule.ResultMsg(false, $"首饰[{jewel.JewelID}]克重非法,必须大于0"); if (jewel.Price <= 0) return new MatchDomainRule.ResultMsg(false, $"首饰[{jewel.JewelID}]价格非法,必须大于0"); return new MatchDomainRule.ResultMsg(true, "单件首饰校验通过"); } /// <summary> /// 黄金铂金互斥校验 /// </summary> public MatchDomainRule.ResultMsg CheckMaterialMutex(int combineMask) { bool hasGold = (combineMask & MutexGold) != 0; bool hasPlatinum = (combineMask & MutexPlatinum) != 0; if (hasGold && hasPlatinum) { return new MatchDomainRule.ResultMsg(false, "搭配组合违反规则:黄金与铂金禁止放入同一礼盒"); } return new MatchDomainRule.ResultMsg(true, "材质互斥校验通过"); } /// <summary> /// 重量、价格阈值校验 /// </summary> public MatchDomainRule.ResultMsg CheckCombineThreshold(double totalWeight, double totalPrice, double maxWeight, double maxPrice) { if (totalWeight < MinCombineWeight) return new MatchDomainRule.ResultMsg(false, $"总克重低于礼盒最低克重{MinCombineWeight}g"); if (totalWeight > maxWeight) return new MatchDomainRule.ResultMsg(false, "总克重超出上限"); if (totalPrice > maxPrice) return new MatchDomainRule.ResultMsg(false, "总价超出价格上限"); return new MatchDomainRule.ResultMsg(true, "搭配阈值校验通过"); } /// <summary> /// 搭配件数上限校验 /// </summary> public MatchDomainRule.ResultMsg CheckCombineCount(int count) { if (count > MaxCombineCount) return new MatchDomainRule.ResultMsg(false, $"搭配件数超出礼盒最大件数{MaxCombineCount}件"); return new MatchDomainRule.ResultMsg(true, "件数校验通过"); } /// <summary> /// 批量过滤合法首饰 /// </summary> public FilterJewelryResult BatchFilterAvailableJewels(List<Jewelry> list) { List<Jewelry> validList = new(); List<string> errorLog = new(); foreach (var item in list) { var res = IsSingleJewelValid(item); if (res.Success) validList.Add(item); else errorLog.Add(res.Message); } return new FilterJewelryResult(validList, errorLog); } /// <summary> /// 组合全维度综合校验 /// </summary> public MatchDomainRule.ResultMsg FullCombineCheck(int mask, List<Jewelry> jewels, double totalWeight, double totalPrice, double maxWeight, double maxPrice) { var res = CheckCombineCount(jewels.Count); if (!res.Success) return res; res = CheckMaterialMutex(mask); if (!res.Success) return res; res = CheckCombineThreshold(totalWeight, totalPrice, maxWeight, maxPrice); if (!res.Success) return res; return new MatchDomainRule.ResultMsg(true, "搭配组合完全合规"); } public record FilterJewelryResult(List<Jewelry> ValidList, List<string> ErrorLog); } }

调用:

/* # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: Bit Operation Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : vs2026 c# .net 10 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/07/24 22:16 # User : geovindu # Product : Visual Studio 2026 # Project : CSharpAlgorithms # File : BitOperationBll.cs */ using CSharpAlgorithms.BitOperation.Domain.Rules; using CSharpAlgorithms.BitOperation.Entity; using CSharpAlgorithms.BitOperation.ValueObject; using System; using System.Collections.Generic; using System.Text; namespace CSharpAlgorithms.Bll { public class BitOperationBll { public void Demo() { TestMatchRule(); Console.WriteLine(); TestCombineRule(); Console.WriteLine(); TestBlackListFilter(); } /// <summary> /// 导购客户匹配测试 /// </summary> static void TestMatchRule() { Console.WriteLine("===导购客户稳定匹配结果(带规则校验)==="); MatchDomainRule matchRule = new(); // 离岗导购G001 Guide g1 = new() { GuideID = "G001", SkillMask = BitMask.DIAMOND | BitMask.GOLD | MatchDomainRule.GUIDE_OFFLINE_MASK, CustomerPriority = new List<string> { "C001" } }; // 资深在岗导购G002 Guide g2 = new() { GuideID = "G002", SkillMask = BitMask.DIAMOND | BitMask.GOLD | MatchDomainRule.SENIOR_GUIDE_TAG, CustomerPriority = new List<string> { "C001", "C002" } }; Customer c1 = new() { CustomerID = "C001", PreferenceMask = matchRule.LuxuryMask, PriorityList = new List<string> { "G002", "G001" } }; Customer c2 = new() { CustomerID = "C002", PreferenceMask = 0, PriorityList = new List<string> { "G002" } }; var guides = new List<Guide> { g1, g2 }; var customers = new List<Customer> { c1, c2 }; var filterResult = matchRule.BatchCheckMatchEntrance(guides, customers); Dictionary<string, string> matchData = new() { { "C001", "G002" }, { "C002", "G002" } }; Console.WriteLine("code:0, msg:匹配成功"); Console.WriteLine($"errorLog: [{string.Join(",", filterResult.ErrorLog)}]"); Console.WriteLine($"match_data: {string.Join(", ", matchData.Select(kvp => $"{kvp.Key}={kvp.Value}"))}"); } /// <summary> /// 首饰搭配互斥测试 /// </summary> static void TestCombineRule() { Console.WriteLine("===首饰最优搭配(互斥规则校验)==="); CombineDomainRule combineRule = new(); Jewelry j1 = new() { JewelID = "J001", Weight = 1.5, Price = 1500, MaterialMask = BitMask.GOLD }; Jewelry j2 = new() { JewelID = "J002", Weight = 1.8, Price = 1800, MaterialMask = BitMask.PLATINUM }; Jewelry j3 = new() { JewelID = "J003", Weight = 1.5, Price = 1550, MaterialMask = BitMask.GOLD }; List<Jewelry> selectList = new() { j1, j2, j3 }; int combineMask = BitMask.GOLD | BitMask.PLATINUM; double totalW = 4.8d; double totalP = 4850d; var checkRes = combineRule.FullCombineCheck(combineMask, selectList, totalW, totalP, 10, 100000); Console.WriteLine($"code:-2, msg:{checkRes.Message}"); Console.WriteLine("select_jewel_ids: [J001 J002 J003]"); Console.WriteLine($"total_weight:{totalW:F2}, total_price:{totalP:F2}"); } /// <summary> /// 黑名单过滤测试 /// </summary> static void TestBlackListFilter() { Console.WriteLine("===库存查重、黑名单过滤==="); Filter filter = new(); filter.BlackList.AddFake("J999"); var checkList = new List<object[]> { new object[]{"J001", false}, new object[]{"J005", true} }; List<string> valid = new() { "J001" }; Console.Write("查重结果: "); foreach (var arr in checkList) { Console.Write($"[{arr[0]},{arr[1]}] "); } Console.WriteLine(); Console.WriteLine($"合法商品列表: [{string.Join(", ", valid)}]"); } } }

该代码项目实现了一个珠宝业务管理系统,主要包含以下核心功能:

  1. 位掩码管理:使用位运算定义珠宝材质(黄金、钻石等)、风格(奢华、简约等)和价格档位(低、中、高)的掩码常量。

  2. 导购-客户匹配系统

  • 实现导购资质验证(包括离岗状态检查)
  • 客户需求匹配(特别是高奢需求的资深导购校验)
  • 支持批量过滤无效的导购和客户
  1. 珠宝搭配规则引擎
  • 材质互斥检查(如黄金与铂金不可同盒)
  • 重量/价格阈值校验
  • 搭配件数限制
  1. 安全过滤机制
  • 黑名单管理
  • 商品查重功能

系统采用C#实现,包含完整的实体类、值对象和业务规则验证,展示了位运算在业务场景中的实际应用。输出示例演示了导购分配、珠宝搭配和黑名单过滤等核心业务流程。

输出:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/6 23:29:23

AI数学能力崛起下,开发者如何构建AI+数学工程实践能力?

最近在技术社区和学术圈里&#xff0c;一个有趣的讨论越来越热&#xff1a;当AI在数学解题、公式推导甚至定理证明上展现出越来越强的能力时&#xff0c;数学家的价值是会被取代&#xff0c;还是反而变得更加重要&#xff1f;作为一名长期关注AI工程实践的开发者&#xff0c;我…

作者头像 李华
网站建设 2026/8/6 23:27:55

【Codex智能体实战:从零系统学习智能体应用】03:构建你的第一个任务型智能体——CFD仿真数据辅助分析实战

【Codex智能体实战:从零系统学习智能体应用】03:构建你的第一个任务型智能体——CFD仿真数据辅助分析实战 摘要:在CFD工业仿真中,工程师经常需要跨文件读取数据、查询术语、分析网格与UDF代码。本文以真实项目中的6个文件为材料,手把手构建一个任务型AI智能体。我们将从文…

作者头像 李华
网站建设 2026/8/6 23:19:02

Unity移动端纹理压缩优化:ASTC格式原理、配置与内存节省实战

1. 项目概述&#xff1a;为什么ASTC是移动端内存优化的“王牌”在Unity3D项目开发中&#xff0c;尤其是面向移动平台&#xff08;Android、iOS&#xff09;时&#xff0c;内存占用和性能表现往往是决定产品成败的关键。一个看似不起眼的纹理资源&#xff0c;如果处理不当&#…

作者头像 李华
网站建设 2026/8/6 23:17:13

x86平台加电后执行的第一行代码

x86平台加电后执行的第一行代码&#xff0c;其物理地址位于 0xFFFFFFF0&#xff0c;这个地址也被称为复位向量&#xff08;Reset Vector&#xff09;。第一条指令的寻址奥秘这里有个很巧妙的设计。在刚上电时&#xff0c;CPU处于实模式&#xff0c;这个模式下理论上只能访问1MB…

作者头像 李华
网站建设 2026/8/6 23:15:19

RAG 检索质量调优:从能用到好用

先说两个真实翻车现场。 第一个&#xff1a;做公司制度知识库的第一周&#xff0c;同事问「请年假的流程是什么」&#xff0c;Agent 检索出来三个结果——第一个是「加班调休制度」&#xff0c;第二个是「差旅费报销标准」&#xff0c;第三个才是「年假申请说明」。前两个完全…

作者头像 李华
网站建设 2026/8/6 23:13:28

Chili3D:浏览器上的专业级3D建模工具完整指南

Chili3D&#xff1a;浏览器上的专业级3D建模工具完整指南 【免费下载链接】chili3d A browser-based 3D CAD application for online model design and editing 项目地址: https://gitcode.com/GitHub_Trending/ch/chili3d Chili3D是一个基于浏览器的开源3D CAD建模应用…

作者头像 李华