BitOperation.cs

简介:
using  System;
using  System.Collections.Generic;
using  System.Text;
 
namespace  Bit
{
     /*class methods for bits operation*/
     public  class  BitOperation
     {
         public  Byte setBit(Byte targetByte, int  targetPos, int  value)
         {
             if  ((value == 0 || value == 1) && (targetPos >= 0 && targetPos < 8))
             {
                 int  targetPosValue = -1;
                 switch  (targetPos)
                 {
                     case  0:
                         targetPosValue = (targetByte >> 7) & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x80);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0x7f);
                             }
                             break ;
                         }
                     case  1:
                         targetPosValue = (targetByte >> 6) & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x40);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0xbf);
                             }
                             break ;
                         }
                     case  2:
                         targetPosValue = (targetByte >> 5) & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x20);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0xdf);
                             }
                             break ;
                         }
                     case  3:
                         targetPosValue = (targetByte >> 4) & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x10);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0xef);
                             }
                             break ;
                         }
                     case  4:
                         targetPosValue = (targetByte >> 3) & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x08);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0xf7);
                             }
                             break ;
                         }
                     case  5:
                         targetPosValue = (targetByte >> 2) & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x04);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0xfb);
                             }
                             break ;
                         }
                     case  6:
                         targetPosValue = (targetByte >> 1) & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x02);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0xfd);
                             }
                             break ;
                         }
                     case  7:
                         targetPosValue = targetByte & 0x01;
                         if  (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                         {
                             break ;
                         }
                         else
                         {
                             if  (value == 1)
                             {
                                 targetByte = Convert.ToByte(targetByte | 0x01);
                             }
                             else  if  (value == 0)
                             {
                                 targetByte = Convert.ToByte(targetByte & 0xfe);
                             }
                             break ;
                         }
                     default :
                         break ;
                 }
                 return  targetByte;
             }
             else
             {
                 return  0;
             }
         }
 
         public  int  getBit(Byte targetByte, int  targetPos)
         {
             int  value = -1;
 
             switch  (targetPos)
             {
                 case  0:
                     value = (targetByte >> 7) & 0x01;
                     break ;
                 case  1:
                     value = (targetByte >> 6) & 0x01;
                     break ;
                 case  2:
                     value = (targetByte >> 5) & 0x01;
                     break ;
                 case  3:
                     value = (targetByte >> 4) & 0x01;
                     break ;
                 case  4:
                     value = (targetByte >> 3) & 0x01;
                     break ;
                 case  5:
                     value = (targetByte >> 2) & 0x01;
                     break ;
                 case  6:
                     value = (targetByte >> 1) & 0x01;
                     break ;
                 case  7:
                     value = targetByte & 0x01;
                     break ;
                 default :
                     break ;
             }
             return  value;
         }
     }
}


本文转自静默虚空博客园博客,原文链接:http://www.cnblogs.com/jingmoxukong/archive/2011/07/29/2121294.html,如需转载请自行联系原作者

相关文章
|
7月前
|
存储 安全 BI
【PACS源码】认识PACS的架构和工作流程
【PACS源码】认识PACS的架构和工作流程
420 0
|
安全 数据库 UED
CS与BS
CS与BS
117 0
|
C# C++ Windows
From.Designer.cs和From.cs的关系
From.Designer.cs和From.cs的关系
|
存储 编解码 数据可视化
开发pacs系统主要解决的问题有哪些?
从计算机技术角度,图像后处理功能的开发和应用影像到整个影像诊断过程。常规的影像处理是必须的,如反相、翻转、调窗、漫游、缩放、旋转、影像冻结、数字减影、标注、划线、距离及角度测量、面积测量、伪彩色等。专业的脱机测量(OFF-LINE)工具也是必要的,如在超声诊断中,提供医生超声设备的所有测量工具,并提供一些超声影像的研究方法等。三维重建技术的使用更利于临床诊断,三维重建方法有Marching Cubes、最大强度投影(MIP)、基于表面的三维显示、基于体绘制的三维显示、内表面绘制的虚拟内窥镜等方法,这些方法在医学影像领域有着广泛的应用前景。
191 0
开发pacs系统主要解决的问题有哪些?
|
中间件 C# C++
[CS]C#操作word
[CS]C#操作word
110 0
|
SQL 安全 Oracle
BS与CS的联系与区别
C/S是Client/Server的缩写。 服务器通常采用高性能的PC、工作站或小型机,并采用大型数据库系统,如Oracle、Sybase、Informix或 SQL Server。 客户端需要安装专用的客户端软件。 B/S是Brower/Server的缩写,客户机上只要安装一个浏览器(Browser),如Netscape Navigator或Internet Explorer,服务器安装Oracle、Sybase、Informix或 SQL Server等数据库。 在这种结构下,用户界面完全通过WWW浏览器实现,一部分事务逻辑在前端实现,但是主要事务逻辑在服务器端实现。 浏览器通过Web
224 0
|
机器学习/深度学习 人工智能 算法
最近发现的几个神奇CS学习网站
自己也是打算学习各种国外的优质课程(最近在学CS224n课程和做对应的project),感觉就可以参考这个北大信科大佬的路线,举例,CS61A课程,
408 0
最近发现的几个神奇CS学习网站