zoukankan      html  css  js  c++  java
  • C# .Net 位运算,字节操作,工具类

    c# 位运算 操作类(提供对字节的位获取,和替换)

    闲来没事写了一个位运算操作的简单类

    1 using System;
    2  using System.Collections.Generic;
    3  using System.Text;
    4
    5 namespace NatureSex.CommonTool.Basic
    6 {
    7 ///<summary>
    8 /// 位 操作工具类(也可以使用MS中System.Collections.BitArray操作)
    9 /// author:NatureSex
    10 ///</summary>
    11 publicsealedclass BitAssist
    12 {
    13 private BitAssist() { }
    14
    15 #region 获取
    16
    17 ///<summary>
    18 /// 取byteSource目标位的值
    19 ///</summary>
    20 ///<param name="byteSource">源字节</param>
    21 ///<param name="location">位置(0-7)</param>
    22 ///<returns>目标值</returns>
    23 publicstaticint GetTargetBit(short location, byte byteSource)
    24 {
    25 Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
    26 return GetTargetBit(location, byteSource, baseNum);
    27 }
    28
    29 ///<summary>
    30 /// 取byteSource目标位的值
    31 ///</summary>
    32 ///<param name="location"></param>
    33 ///<param name="byteSource"></param>
    34 ///<param name="baseNum">与 基数(1,2,4,8,16,32,64,128)</param>
    35 ///<returns></returns>
    36 privatestaticint GetTargetBit(short location, byte byteSource, byte baseNum)
    37 {
    38 if (location >7|| location <0) return-1000;
    39 return (byteSource & baseNum) == baseNum ?1 : 0;
    40 }
    41
    42 ///<summary>
    43 /// 取一批byteSources目标位的对应的值集合
    44 ///</summary>
    45 ///<param name="location">位置(0-7)</param>
    46 ///<param name="byteSources">一批字节</param>
    47 ///<returns>一一对应的目标值</returns>
    48 publicstaticint[] GetTargetBit(short location, paramsbyte[] byteSources)
    49 {
    50 if (byteSources ==null) returnnull;
    51 int bsLen = byteSources.Length;
    52 Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
    53 int[] result =newint[bsLen];
    54 for (int i =0; i < bsLen; i++)
    55 {
    56 result[i] = GetTargetBit(location, byteSources[i], baseNum);
    57 }
    58 return result;
    59 }
    60
    61 #endregion
    62
    63 #region 替换
    64
    65 ///<summary>
    66 /// 替换byteSource目标位的值
    67 ///</summary>
    68 ///<param name="location">替换位置(0-7)</param>
    69 ///<param name="value">替换的值(1-true,0-false)</param>
    70 ///<param name="byteSource">源字节</param>
    71 ///<returns>替换后的值</returns>
    72 publicstaticbyte ReplaceTargetBit(short location, bool value, byte byteSource)
    73 {
    74 Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
    75 return ReplaceTargetBit(location, value, byteSource, baseNum);
    76 }
    77
    78 ///<summary>
    79 /// 替换byteSource目标位的值
    80 ///</summary>
    81 ///<param name="location"></param>
    82 ///<param name="value">替换的值(1-true,0-false)</param>
    83 ///<param name="byteSource"></param>
    84 ///<param name="baseNum">与 基数(1,2,4,8,16,32,64,128)</param>
    85 ///<returns></returns>
    86 privatestaticbyte ReplaceTargetBit(short location,bool value,byte byteSource, byte baseNum)
    87 {
    88 if (location >7|| location <0)
    89 {
    90 thrownew FormatException("location params error!type range(0-7)");
    91 }
    92 bool locationValue=GetTargetBit(location, byteSource)==1?true:false;
    93 if (locationValue != value)
    94 {
    95 return (byte)(value ? byteSource + baseNum : byteSource - baseNum);
    96 }
    97 return byteSource;
    98 }
    99
    100 ///<summary>
    101 /// 替换一批byteSources目标位的对应的值集合
    102 ///</summary>
    103 ///<param name="location">替换位置(0-7)</param>
    104 ///<param name="value">替换的值(1-true,0-false)</param>
    105 ///<param name="byteSources">一批字节</param>
    106 ///<returns>替换后一一对应的值</returns>
    107 publicstaticbyte[] ReplaceTargetBit(short location, bool value, paramsbyte[] byteSources)
    108 {
    109 if (byteSources ==null) returnnull;
    110 int bsLen = byteSources.Length;
    111 Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
    112 byte[] result =newbyte[bsLen];
    113 for (int i =0; i < bsLen; i++)
    114 {
    115 result[i] = ReplaceTargetBit(location, value, byteSources[i], baseNum);
    116 }
    117 return result;
    118 }
    119
    120 #endregion
    121 }
    122 }
  • 相关阅读:
    Dreamweaver采用utf8制作页面,到.net显示乱码问题解决
    看不完的风景,走不完的路
    整个世界都在返利
    Google 地图小工具:让别人找到你
    开心网
    提取国家地理图片总结
    [脚本收集]:在线词典
    提取国家地理图片总结之二
    [脚本收集]提取国家地理图片
    若我离去,后会无期
  • 原文地址:https://www.cnblogs.com/NatureSex/p/2094856.html
Copyright © 2011-2022 走看看